Geek
Geek

Reputation: 23419

Java Classpath problem

I have a Java Program that references a Jar File.

The Jar File is present in the same directory as the .class File but when I try to run the program from console, I get NoClassDefFound error :-(

Alternatively it runs Ok from Eclipse.

Why ?

Upvotes: 1

Views: 9518

Answers (2)

NullPointerException
NullPointerException

Reputation: 184

JAR files are not automatically included in the classpath. You can add a Class-Path entry and a Main-Class entry to the to JAR file containing the main method that you wish to execute. Then you can execute your code like so:

java -jar yourJarFile.jar

See the JAR File Specification for more details.

or specify the classpath on the command line:

java -classpath lib1.jar:lib2.jar:yourJarFile.jar your.MainClass

Upvotes: 3

Adam Paynter
Adam Paynter

Reputation: 46908

The java program will not automatically include JAR files in its class path. You add a JAR file to its class path via the -classpath option. For example:

java -classpath .;yourJarFile.jar your.MainClass

Eclipse is automatically adding the JAR file whenever you run the program.

Note: The -classpath option expects a semicolon (;) when running on Windows and a colon (:) otherwise.

Upvotes: 7

Related Questions