Solved

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

Posted on 2002-07-09
9
229 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
firstChar challenge 13 85
Requested array size exceeds VM limit 3 50
Java and GPO 11 47
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now