0

I have a simple HelloWorld program in Eclipse. I run it on the IDE and everything works fine. Now I wish to see the same helloworld program output on the command line, I see the below:

Error: could not find or load main class TestCode
Caused by: java.lang.ClassNotFoundException: TestCode

I have a project structure in the eclipse workspace: project-> src-> package1->TestCode.java

I can reach upto package1 directory and then successfully perform :

javac TestCode.java

I see that the TestCode.class file is created as well

but when i perform the run, I get the error as shown

java TestCode
Error: could not find or load main class TestCode
Caused by: java.lang.ClassNotFoundException: TestCode

ClassPath variables are all checked.

I tried with exporting a jar and running it on the command line. It works.

what is the error here? Am I doing anything wrong or any theory I am missing? Please let me know

  • You need to set the classpath to point to a directory *containing* a directory `package1` which in turn *contains* a file called `TestCode.class` and *then* you must run `java package1.TestCode`. The `java` executable expects the **fully-qualified class name** of the class to launch. Just to double-check: check the `META-INF/MANIFEST.MF` file in the jar file you generated: Read what it states as the `Main-Class` attribute and see for yourself. – Joachim Sauer Jul 21 '21 at 09:08
  • @JoachimSauer, I observed the same error again. So in the project structure, I go to src-> open a command prompt--> execute" java package1.TestCode " I get the same error! – Rakshan Premsagar Kapikad Jul 21 '21 at 09:12
  • Read the question linked-to above, it explains it all in much detail. Specifically you need to check that your CLASSPATH points to the correct place (personally I suggest always using the `-cp` switch explicitly instead of relying on the `CLASSPATH` environment variable). – Joachim Sauer Jul 21 '21 at 09:15

0 Answers0