Evan_HZY
Evan_HZY

Reputation: 1024

Having a trouble on java.lang.NoClassDefFoundError

I have a test.jar package and a Test.java file in the same folder(it is not in my CLASSPATH).

In the .java file I import some classes that are in the .jar package.
So I go the that folder, use java -classpath test.jar Test.java to compile. Everything looks good. Then I use java Test, it gives me an error:

Exception in thread "main" java.lang.NoClassDefFoundError:......

saying that it cannot find the classes inside the .jar file.

I move the file to Eclipse and add the jar file, it works.

Upvotes: 1

Views: 454

Answers (2)

Anantha Sharma
Anantha Sharma

Reputation: 10108

just as you compiled the code using javac -classpath test.jar Test.java you should use the java command to run (add the jar file in the classpath) java -classpath test.jar Test.java

Upvotes: 1

Marcelo
Marcelo

Reputation: 4608

You must run the java command with the classpath too, same as the compiler.

Upvotes: 1

Related Questions