73

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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985

4 Answers4

117

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.

drevicko
  • 14,382
  • 15
  • 75
  • 97
Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
  • I searched forever to figure this out! – chotchki Apr 20 '10 at 16:21
  • 9
    Just to be clear, the jar has to explicitly in the classpath. Specifying a /lib dir is inefficient. – cmcginty Feb 10 '11 at 00:08
  • 12
    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 ";"/":" – greenoldman Dec 01 '11 at 09:59
  • 2
    "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` – Kuba Dec 02 '13 at 12:05
  • You can specify dir like this. java -classpath .:myjar.jar:lib/* my.package.MainClass – mac Jan 28 '14 at 15:19
  • 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\!/').openConnection()).getMainAttributes().getValue(java.util.jar.Attributes.Name.MAIN_CLASS))"` – Janus Troelsen Nov 03 '14 at 22:29
60

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.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • 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. –  Jun 03 '09 at 17:39
  • 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... – Michael Myers Jun 03 '09 at 17:53
  • 1
    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. – Daniel Nesbitt Jun 03 '09 at 19:03
  • 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 – Stefan Haberl Mar 12 '14 at 08:52
  • 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. – Markos Apr 15 '14 at 17:56
13

try

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

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

Premraj
  • 72,055
  • 26
  • 237
  • 180
artemb
  • 9,251
  • 9
  • 48
  • 68
3

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".

cupakob
  • 8,411
  • 24
  • 67
  • 76
  • I don't see a "Runnable jar" option in the export windows, just a JAR file :( –  Jun 03 '09 at 17:26
  • which version of eclipse are you using? i have it in the Ganymede....screrenshot here: http://bufka.ath.cx/dslr/Eclipse-Export.png But i dont know, if that is default settings or not. – cupakob Jun 03 '09 at 20:17