Java: how to import a jar file from command line

mrmuggles picture mrmuggles · Jun 3, 2009 · Viewed 213.6k times · Source

I'm trying to call a class (main method) from command line (Windows) with Java.
The class imports other classes (other jars).

I always get "class not found exception" from a class that my main program imports.

Here's what I tried:

  • Add a CLASSPATH env. var with the path where the referenced lib resides (not working)

  • I tried with all these different parameters when calling "java -jar myjar.jar" from command line : "-classpath lib/", "-classpath ./lib/", "-classpath lib", "-cp lib/*", "-cp lib/\*", "-classpath lib/referenced-class.jar", "-classpath ./lib/referenced-class.jar" (lib is where the referenced jar resides)

  • I tried packaging all the referenced jar inside my jar where my main class resides...

  • And with all that, I also tried to specify the classes inside the Manifest file with: Class-path referenced-jar.jar and I also tried Class-path lib/referenced-jar.jar

Answer

Adam Paynter picture Adam Paynter · Jun 3, 2009

You could run it without the -jar command line argument if you happen to know the name of the main class you wish to run:

java -classpath .;myjar.jar;lib/referenced-class.jar my.package.MainClass

If perchance you are using linux, you should use ":" instead of ";" in the classpath.