Solved

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

Posted on 2002-07-09
9
239 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
MySQL  on Tomcat 8 70
jboss 7.1 start up error 1 41
Bot application - advice 3 39
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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