Forums Register Login

java command not finding jar file

+Pie Number of slices to send: Send
Hi,

I have a class that uses packages inside a jar file.

The class compiles but when I try to run java I get the error message ClassNotFoundException.

Here are the commands:

C:\Users\Marlon\workspace\javacow>javac DaysOld.java -cp ./jr.jar

C:\Users\Marlon\workspace\javacow>java DaysOld 2007/3/17
Exception in thread "main" java.lang.NoClassDefFoundError: com/javaranch/common/
GDate
at DaysOld.main(DaysOld.java:9)
Caused by: java.lang.ClassNotFoundException: com.javaranch.common.GDate
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

C:\Users\Marlon\workspace\javacow>java -cp ./jr.jar DaysOld 2007/3/17
Exception in thread "main" java.lang.NoClassDefFoundError: DaysOld
Caused by: java.lang.ClassNotFoundException: DaysOld
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: DaysOld. Program will exit.



In the first command it seems java can't find the GDate class, and in the second example java can't find the DaysOld class. It seems odd it can compile but can't run. DaysOld and jr.jar are in the javacow directory.
+Pie Number of slices to send: Send
Try the following:
Options go before the source file(s).
By using -cp the current directory is no longer automatically part of the class path. You need to add it manually. That's what the extra ".;" is for.

Also, in both examples: either use .\jr.jar or just jr.jar. A slash (/) doesn't work within the Windows command prompt.
+Pie Number of slices to send: Send
 

Rob Spoor wrote:Try the following:
Options go before the source file(s).
By using -cp the current directory is no longer automatically part of the class path. You need to add it manually. That's what the extra ".;" is for.

Also, in both examples: either use .\jr.jar or just jr.jar. A slash (/) doesn't work within the Windows command prompt.



Thanks! The option -cp .;jr.jar worked! Didn't know that about the / in windows - thought they were interchangeable for some reason.
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1934 times.
Similar Threads
running with command line
How to make this packages example program work
JDate and GDate
errors with main class
java unable to recognize class where my IDE does
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:23:42.