• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

java command not finding jar file

 
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.
 
Sheriff
Posts: 21787
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
I was born with webbed fish toes. This tiny ad is my only friend:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!