PDA

View Full Version : Java Classpath problem


ubermike
August 29th, 2007, 11:09 AM
I have created a jar(test.jar) that relies on a third party library(jgraph.jar). My test.jar has the following manifest:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_11-b03 (Sun Microsystems Inc.)
Built-By: ubermike
Main-Class: org.ubermike.TestJGraph
Class-Path: jgraph.jar

When I try to run it using any of the following formats I get the same error - NoClassDefFound:

java -jar test.jar
java -cp ./lib/jgraph.jar -jar test.jar
java -classpath ./lib/jgraph.jar -jar test.jar

I have tried setting the CLASSPATH with no effect:
export CLASSPATH=./lib/jgraph.jar or export CLASSPATH=./lib

Does anybody have any ideas?

samjh
August 29th, 2007, 11:44 AM
The Class-Path option in your manifest needs to specify the path to your jgraph.jar file.

So if your jgraph.jar file is located in ./lib relative to your test.jar file, it should be:Class-Path: ./lib/jgraph.jar

If you're running the test.jar file by hand, do this:
java -cp ./lib/jgraph.jar:. -jar test.jar

When you specify classpaths explicitly, you need to specify the locations of the dependencies, as well as the class you want to run.

ubermike
August 29th, 2007, 11:52 AM
Made change to MANIFEST but get the same results.

I have also reviewed the contents of the jgraph.jar to confirm it does contain the class that I require.

Any other ideas?

It appears that no matter what I do the classpath is disregarded.

samjh
August 29th, 2007, 11:54 AM
Exactly what is in your test.jar file? How are the files organised within?

ubermike
August 29th, 2007, 12:00 PM
test.jar :
META-INF/MANIFEST.MF
org/ubermike/TestJGraph.class
lib/jgraph.jar

This version has the "lib/jgraph.jar" but it should not make a difference. I have tried it without.

samjh
August 29th, 2007, 12:19 PM
You cannot access a jar file stored within a jar file.

What you need to do is take the jgraph.jar file out of there, and put it into a directory named /lib. Then your manifest can say Class-Path ./lib/jgraph.jar and it will work.

Otherwise, extract everything in jgraph.jar, and then re-pack the TestJGraph class into a /org/ubermike/ directory within your test.jar file. Your manifest should then specify Class-Path /org/ubermike/TestJGraph (I think, I haven't done this in ages).

As an example of the first method, here's what a recursive directory listing (using ls -R) looks like in a small project of mine:
.:
gametute.jar lib

./lib:
GTGE.jar player.pnggametute.jar contains my main class: GameTute.class file. But it depends on GTGE.jar, which resides in a /lib directory outside of my gametute.jar, because Java doesn't allow you to access a jar within a jar.

ubermike
August 29th, 2007, 12:33 PM
Sorry, no luck. It still throws same exception

~/Projects/TestJGraph$ java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgraph/graph/GraphModel
~/Projects/TestJGraph$ java -cp .:./lib/jgraph.jar -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgraph/graph/GraphModel

I have attached my test.jar (had to name it test.zip)

ubermike
August 29th, 2007, 12:51 PM
The MANIFEST.MF identifies that the jar was built with 1.5.0_11-b03 (Sun Microsystems Inc.)

Below is the output from java -version on command line:
~/Projects/TestJGraph$ java -version
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

samjh
August 29th, 2007, 01:11 PM
To clarify: your program is TestJGraph, right? TestJGraph belongs to org.ubermike package? And TestJGraph depends on the jgraph.jar library?

If my assumptions are true, your test.jar file and its manifest looks OK.

What I find puzzling is this:
~/Projects/TestJGraph$ java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgraph/graph/GraphModelWhy is Java trying to locate the "main" method in org.jgraph.graph.GraphModel?! According to your manifest, it should be pointing to org.ubermike.TestJGraph!

I need to sleep. Hopefully someone with a fresher mind will help you out soon enough. ;)

ubermike
August 29th, 2007, 02:15 PM
Yes, the program is TestJGraph.

Actually, it is running main from TestJGraph but the problem arises from the following line:

GraphModel model = new DefaultGraphModel();

That is the first line in the main method.

pbrockway2
August 30th, 2007, 04:33 AM
What is the manifest you are using?

It is important to confirm that you are running the main method of the class you think you are, and that the classpath is what you think it is. This information comes from the manifest. (and the manifest is fussy about things like line length and new lines.)

See, for example http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html

Incidently, I don't think the -cp switch has any effect if you use the -jar switch. Ie, the classpath is exactly what the Class-Path manifest entry says it is.

ubermike
August 30th, 2007, 09:53 AM
Attached is my MANIFEST.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_11-b03 (Sun Microsystems Inc.)
Built-By: ubermike
Main-Class: org.ubermike.TestJGraph
Class-Path: ./lib/jgraph.jar



Note : There are actually two blank lines after the Class-Path line.

Actually, I had already read that the -cp on the command line should have not effect. I was trying it because nothing else seems to be working.

pbrockway2
August 31st, 2007, 07:28 AM
Sorry, I've just realised that you already posted the manifest...

Anyway I downloaded jgraph, and your jar file and had a play. I got the same error as you - even tried lots of variations with the manifest. It's late here, so maybe I'm missing something obvious.

The following, however, does work fine. It doesn't use the manifest, and is essentially what they say to do on the jgraph web site

java -cp ./test.jar:./lib/jgraph.jar org.ubermike.TestJGraph

(I don't post here much so I hope I get the code tags right.)