Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java command not finding jar file

 
Marlon Churchill
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marlon Churchill
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic