jar running problems

A

andrew123

Hi,

I am really suffering with trying to execute a class from within a jar
file.
What I've done:-
I have created a jar called tester.jar, within this jar I have a
directory structure of
com.amm.mar.bo.gh at the bo level I have my main class that supposed to
run called tester.class
I have other class files that are referenced at the gh level.I also
reference the external packages of log4j and jdom(seperate jars in same
dir)

When I try to run this jar I get a

Exception in thread "main" java.lang.NoClassDefFoundError: tester/java

I run it by doing the following commands:-

java -cp ./:tester.jar:./log4j.jar:/jdom.jar -jar tester.jar
com.amm.mar.bo.tester

I've also tried variants of this by having differnt classpaths, but to
no avail. I created a manifest file as well
which reads as:-

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.4.2_03-b02 (Sun Microsystems Inc.)
Version: 1.0
Main-Class: com.amm.mar.bo.tester
Class-Path: jdom.jar:log4j-1.2.9.jar:jconn3.jar:tester.jar


The strange thing is it all works in eclipse and was built using
ant....

I've tried bith winxp and solaris...

Any ideas what I'm doign wrong ?
 
A

Andrew Thompson

I am really suffering with trying to execute a class from within a jar
file.

The standard Java loading mechanisms will not find resources in
a jar inside a jar. You *could* extract the jar out at installation
time and later ensure it is on the classpath, but that is a separate
issue to what I suspect you are trying to do. Don't do it.

Either unjar the classes and rejar them into the final jar, or
add the jar to the classpath of the application/applet.
The strange thing is it all works in eclipse..

( insert spooky music ;)
 
A

andrew123

Hi,

I've not done much work with jar files, so forgive me if this should be
obvious.

Are you saying that since I have a directory structure that has class
files at different levels the compiler can't "see" them all ?

The other jar files I am referencing (log4j, jdom, jconn3) are external
jars referenced in the standard package import way. So, my jar file
tester.jar has no jars within it....

Am I missing something super obvious ?

Cheers,

Abu
 
A

Andrew Thompson

I've not done much work with jar files, so forgive me if this should be
obvious.

Ugghh.. sorry. I *completely* misread your original sentence
as 'jar within jar'. That sent me on a wrong tangent.
I'll have a closer look at what you wrote and see if I can
make some more(?) sense.
 
R

Roland

Hi,

I am really suffering with trying to execute a class from within a jar
file.
What I've done:-
I have created a jar called tester.jar, within this jar I have a
directory structure of
com.amm.mar.bo.gh at the bo level I have my main class that supposed to
run called tester.class
I have other class files that are referenced at the gh level.I also
reference the external packages of log4j and jdom(seperate jars in same
dir)

When I try to run this jar I get a

Exception in thread "main" java.lang.NoClassDefFoundError: tester/java

I run it by doing the following commands:-

java -cp ./:tester.jar:./log4j.jar:/jdom.jar -jar tester.jar
com.amm.mar.bo.tester

I've also tried variants of this by having differnt classpaths, but to
no avail. I created a manifest file as well
which reads as:-

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.4.2_03-b02 (Sun Microsystems Inc.)
Version: 1.0
Main-Class: com.amm.mar.bo.tester
Class-Path: jdom.jar:log4j-1.2.9.jar:jconn3.jar:tester.jar


The strange thing is it all works in eclipse and was built using
ant....

I've tried bith winxp and solaris...

Any ideas what I'm doign wrong ?
Instead of the colon ':', try to use spaces as separator between the
entries of Class-Path attribute. And the reference to 'tester.jar' (to
itself) is not needed:

Class-Path: jdom.jar log4j-1.2.9.jar jconn3.jar

<http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Main Attributes>

Like you already mentioned, jdom.jar log4j-1.2.9.jar jconn3.jar should
be in the same directory as tester.jar.

Then the following commandline should be enough to start your
application (provided no other classes are missing, and the class
com.amm.mar.bo.tester contains the main method):
java -jar tester.jar

Note, that when you use the -jar option, other class path settings on
the commandline or in the CLASSPATH environment variable are ignored.
<http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#options>
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
J

John Currier

You were probably on the right track with your Class-Path entry, but
you're supposed to have one or more spaces between the elements.
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html

The reason it works under Eclipse is that Eclipse is running the class
and not the jar. Executable jars don't inherit (or allow command-line
specification of) a classpath.

In SchemaSpy (http://schemaspy.sourceforge.net) I use a URLClassLoader
to dynamically do what you're attempting (classpath varies depending on
the database driver you want to use). I also use a URLClassLoader to
allow a "-cp" option to emulate java's -cp ability.

John
 
A

andrew123

Fantastic !!! Thank you Guys.

The spaces fixed the problem I was having....

Any chance one of you guys might be able to sort Scotland out with a
goal against Belarus ? :)

Thanks Everyone.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top