Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-07-09
9
Medium Priority
?
254 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 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