Java command line issues with .jar libraries

I have one .java file (driver.java) that I am trying to compile and run from the command line. It uses an external library called EXT.jar , the structure of which is just a folder named EXT with dozens of classes inside it.

So, I run:

 javac -cp EXT.jar driver.java 

This compiles the class just fine.

then at startup:

 java -cp EXT.jar driver 

I get java.lang.NoClassDefFoundError .

Oddly enough, if I unzip the JAR (now I have a folder in the EXT root directory), the last command works just fine! The driver will be executed!

Is there any way to get driver.class to look for needs class files from EXT.jar/EXT/*class instead of the actual EXT folder?

Thanks!

+7
java command-line jar compilation
source share
1 answer

You compile the class into a local directory. Therefore, when you run it, you need to include the current directory in your classpath. For example:.

 java -cp .;EXT.jar driver 

Or in linux:

 java -cp .:EXT.jar driver 

Now that you have this, you say that your class path is only EXT.jar (along with what is in the CLASSPATH environment variable) and nothing else (therefore, the directory where driver.class is located is excluded)

+18
source share

All Articles