Java: how to import a jar file from command line

215,080

Solution 1

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.

Solution 2

If you're running a jar file with java -jar, the -classpath argument is ignored. You need to set the classpath in the manifest file of your jar, like so:

Class-Path: jar1-name jar2-name directory-name/jar3-name

See the Java tutorials: Adding Classes to the JAR File's Classpath.

Edit: I see you already tried setting the class path in the manifest, but are you sure you used the correct syntax? If you skip the ':' after "Class-Path" like you showed, it would not work.

Solution 3

try

java -cp "your_jar.jar:lib/referenced_jar.jar" com.your.main.Main

If you are on windows, you should use ; instead of :

Solution 4

you can try to export as "Runnable jar" in eclipse. I have also problems, when i export as "jar", but i have never problems when i export as "Runnable jar".

Share:
215,080
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • Admin
    Admin almost 15 years
    I don't see a "Runnable jar" option in the export windows, just a JAR file :(
  • Admin
    Admin almost 15 years
    yes, I tried with the ":" also... I don't know why using the Manifest to specify those didn't worked at all. I asked collegues and rechecked everything with them.
  • palantus
    palantus almost 15 years
    Most curious. I've done it this way for years and never had a problem. Did you capitalize Class-Path properly? I don't know if it would make a difference...
  • Daniel Nesbitt
    Daniel Nesbitt almost 15 years
    I feel that this is best answer. Making a Manifest file just right manually is often a difficult task. I usually unpackage the jar and have Ant make another one. It is possible that his path and resource names are longer than 72 characters which causes problems if not properly wrapped.
  • cupakob
    cupakob almost 15 years
    which version of eclipse are you using? i have it in the Ganymede....screrenshot here: bufka.ath.cx/dslr/Eclipse-Export.png But i dont know, if that is default settings or not.
  • chotchki
    chotchki about 14 years
    I searched forever to figure this out!
  • cmcginty
    cmcginty about 13 years
    Just to be clear, the jar has to explicitly in the classpath. Specifying a /lib dir is inefficient.
  • greenoldman
    greenoldman over 12 years
    Thank you very much, my first Scala "hello world" jar is alive ;-) For the record -- in Unix you use ":" instead of ";", and in case of more lib jars you simply add more jar filenames separated with ";"/":"
  • Kuba
    Kuba over 10 years
    "if you happen to know the name of the main class". You can easly find out with unzip -p the_jar.jar META-INF/MANIFEST.MF | grep Main-Class
  • mac
    mac over 10 years
    You can specify dir like this. java -classpath .:myjar.jar:lib/* my.package.MainClass
  • Stefan Haberl
    Stefan Haberl about 10 years
    I wouldn't go down this line if you are dealing with third party jars you want to run. Go for Adam's answer instead
  • Markos
    Markos about 10 years
    This should be the accepted answer. I wonder if it's possible to add all jars from directory. Using dir/* and dir/*.jar did not work.
  • Janus Troelsen
    Janus Troelsen over 9 years
    another way to find the main-class: if your file is in $PWD and is named "ThoreInSpace.jar": groovy -e "System.out.println(((JarURLConnection) new URL('jar','','file://$PWD/ThoreInSpace.jar\!/').openConnecti‌​on()).getMainAttribu‌​tes().getValue(java.‌​util.jar.Attributes.‌​Name.MAIN_CLASS))"