Solved

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

Posted on 2002-07-09
9
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
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…
Suggested Courses

752 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