Solved

ClassDefNotFound error when running jar, but works when running class files!

Posted on 2002-07-09
9
236 Views
Last Modified: 2013-11-23
This ones doing my nut!
I have a simple single class app which runs fine when I execute its .class files, but when I put them in a jar it can't find classes it uses in other jars (eg XML parsers etc). I'm executing from the same directory, with the same classpath, etc in both cases. My app is executing in both cases (ie its not my MANFIEST Main-Class entry thats a problem).

Occurs on both Solaris & Win2000.
JDK 1.3.1_02

Any ideas appreciated.

TIA Colm.
0
Comment
Question by:ColmOhEocha
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7140198
if you are using the manifest Main-Class and start the application with the java -jar file you must use
Class-Path: parameter of the manifest in order to set the class path. the -classpath parameter is ignored in this case
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7140203
"I'm executing from the same directory, with the same classpath" - Add to the classpath the jar which contains your class too.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7140206
Something like :

java -cp .;<"external jars">;myApplicationJAR.jar com.example.MYApplication
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ColmOhEocha
ID: 7140276
Using the Class-Path header in the manifest fixes the problem, but:
1) In the online java.sun.com docs, it states the Class-Path header is part of the 'download extension' mechanism and need only be used if the extension jar file is not on the classpath. It doesn't say anywhere (ie in any books, websites, etc that I can find) that the -classpath is ignored when using the java -jar option. Is this a bug? Is the documentation wrong?
2) If indeed the -classpath is ignored for jars, this is a major portability issue - it means the jar must have the location of extension jars 'hardcoded' into the manifest - but I don't know where these will be on the target machine!
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 total points
ID: 7140328
1) as you can see by running  java -help - java -jar option desn't have -classpath parameter.
2) you can specify the jars, without thir path - then the jars will be searched in the current directory.
3) if you want to use -classpath option don't start you application with -jar option:
java -jar MyArc.jar

but use
java -classpath MyArc.jar;...; <main-class>
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7140331
Hi,

In your application:
do you use custom class loader or do you load class in dynamic way?
for example:
Class.forName(<class name>);

Best regards
Nir
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7140345
please disregard my previous note.


0
 

Author Comment

by:ColmOhEocha
ID: 7140387
OK - I get it - I thought you had to use the -jar option to run an app in a jar file - may seem silly but the java -help is not 100% clear.

Thanks for your help, Colm.


On a small point Java help does seem to indicate -classpath is a valid option when using -jar, and I still can't find any docs that say the system CLASSPATH is ignored:
C:\JDK13~1.1_0\bin>java -help
Usage: java [-options] class [args...]
           (to execute a class)
   or  java -jar [-options] jarfile [args...]
           (to execute a jar file)

where options include:
    -hotspot      to select the "hotspot" VM
    -server       to select the "server" VM
    -classic      to select the "classic" VM
                  If present, the option to select the VM must be first.
                  The default VM is -hotspot.

    -cp -classpath <directories and zip/jar files separated by ;>
                  set search path for application classes and resources
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -showversion  print product version and continue
    -? -help      print this help message
    -X            print help on non-standard options
0
 
LVL 92

Expert Comment

by:objects
ID: 7142156
From the java documentation for the -jar option:

"... When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. "

http://java.sun.com/j2se/1.4/docs/tooldocs/windows/java.html
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
HTTPSessionBindingListernter not configured in deployment descriptor 2 52
print map entry 34 67
Desingning Refactoring existing code 2 24
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question