jar access problem

A

asd

Hi all,

My application is something like this:

I have two files:
c:\test\a\b\JarTest.class
c:\test\c\d\JarMan.class

The java code is as follows:

JarTest.java
------------
package a.b;

import c.d.JarMan;

public class JarTest{
public static void main(String asd[]){
JarMan jm = new JarMan();
jm.testFunc();
}
}

JarMan.java
------------

package c.d;

public class JarMan{
private int a;

public void testFunc(){
System.out.println("print:: "+this.a);
}
}

Now I created a jar file out of c/d/JarMan.class with the correct
directory structure called jarman.jar.

And created an exceutable jar file out of a/b/JarTest.class called
jartest.jar.

The manifest file in jartest.jar looks like this:

Manifest-Version: 1.0
Created-By: 1.4.2_04 (Sun Microsystems Inc.)
Main-Class: a.b.JarTest

Now from within the c:\test folder I try to execute jartest.jar and
this is what happens:

C:\test>java -classpath c:\test\jarman.jar; -jar jartest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: c/d/JarMan
at a.b.JarTest.main(JarTest.java:7)

Strange isnt it?

Now when I extract the contents of jartest.jar and try it works:
C:\test>java -classpath c:\test\jarman.jar; a/b/JarTest
print:: 0

My head is spinning with this problem :-(

Help me out!!!
Thanks in advance.

regards,
ASD
 
F

Fahd Shariff

You cannot have both -classpath and -jar as path of the java command.
"When you use -jar, the JAR file is the source of all user classes, and
other user class path settings are ignored." This is why it cant find
the JarMan class because the jarman jar file is being ignored.

What you need to do is add the Class-Path attribute to the manifest:

Class-Path: jarman.jar
 
J

John C. Bollinger

asd wrote:

[...]
Now I created a jar file out of c/d/JarMan.class with the correct
directory structure called jarman.jar.

And created an exceutable jar file out of a/b/JarTest.class called
jartest.jar.

The manifest file in jartest.jar looks like this:

Manifest-Version: 1.0
Created-By: 1.4.2_04 (Sun Microsystems Inc.)
Main-Class: a.b.JarTest

Now from within the c:\test folder I try to execute jartest.jar and
this is what happens:

C:\test>java -classpath c:\test\jarman.jar; -jar jartest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: c/d/JarMan
at a.b.JarTest.main(JarTest.java:7)

Strange isnt it?

Not what you might expect, perhaps, but it is the documented behavior.
The documentation of the -jar option to the java program includes this:
"When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored." (See
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#-jar)

This is not arbitrary or unreasonable behavior; rather, it neatly
sidesteps potential problems with incompatible versions of packages
outside the platform library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top