my dog learned polymorphism*
The moose likes Beginning Java and the fly likes java command not finding jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java command not finding jar file " Watch "java command not finding jar file " New topic
Author

java command not finding jar file

Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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.


Let the Washing Machine Tribe conquer all!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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.
 
Consider Paul's rocket mass heater.
 
subject: java command not finding jar file
 
Similar Threads
errors with main class
JDate and GDate
How to make this packages example program work
java unable to recognize class where my IDE does
running with command line