File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Pain with classpaths.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pain with classpaths.." Watch "Pain with classpaths.." New topic

Pain with classpaths..

Chris Sleep

Joined: Apr 05, 2006
Posts: 3

This is not a java language problem per se, but I couldn't spot a packaging forum..

I'm playing with hibernate and have built a simple utility packagewhich I was intending to use to replace an old broken perl script on a unix host.

I've rolled the package up into a simple jar file to try to keep it clean, and what I'd like to do is to run it via a simple shell script so that I can pipe away the output and do other things with it.

However what I get when I invoke the shell script is an exception relating to one of the imported libraries: java.lang.NoClassDefFoundError: org/hibernate/Session

The script reads:


/usr/jdk/latest/bin/java -classpath /usr/local/classes/hibernate/antlr-2.7.6rc1.jar:/usr/local/classes/hibernate/asm.jar:/usr/local/classes/hibernate/cglib-2.1.
.2.11.jar:/usr/local/classes/hibernate/tds.jar -jar mailnames.jar

the packaged MANIFEST.MF reads:
Manifest-Version: 1.0
Main-Class: MailnamesProcessor

In the same folder, I have an ant build.xml file which contains:

<project name="mailnames" default="run">

<property name="librarydir" value="/usr/local/classes/hibernate"/>

<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>

<target name="run">
<java fork="true" classname="MailnamesProcessor" classpathref="libraries">
<classpath path="mailnames.jar"/>

If I invoke ant the application runs perfectly - except that ant wraps the output with its target statements which then make the output useless for piping elsewhere.

I'm sure I must be missing something blindingly obvious, but I'm at a loss for what, any advice will be gratefully received.

Eduardo Dela Rosa

Joined: Apr 15, 2004
Posts: 25
How about prepending your classpath argument with .:

thus, your classpath definition should look like this:

[ April 05, 2006: Message edited by: Eduardo Dela Rosa ]
[ April 05, 2006: Message edited by: Eduardo Dela Rosa ]
Chris Sleep

Joined: Apr 05, 2006
Posts: 3
Just tried that one and it makes no difference I'm afraid, and I'm not sure I'd expect it to - the org.hibernate.Session class should be found in /usr/local/classes/hibernate/hibernate3.jar, at least, that's what I'm expecting!
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

When you use the -jar option when running java on the command line, the -classpath option is completely ignored. So all that typing you did was for naught. If you want those jars to be in the classpath, you have to put them in the manifest of your executable jar file (as Class-Path: entries I believe).

Or alternatively don't use the -jar option, and just make your executable jar file one of the many in the classpath.
Eduardo Dela Rosa

Joined: Apr 15, 2004
Posts: 25
Yes, Paul is right, because the manifest file (MANIFEST.MF) is the file
where you declare information about the files packaged in your JAR file.

An example entry for a manifest file might be:


Manifest-Version: 1.0
Created-By: 1.5.0_05 (Sun Microsystems Inc.)
Main-Class: tiger.autoboxing.Wrapper
Class-Path: /usr/local/jboss/server/default/lib/log4j.jar


where you declare all dependency jar files in the Class-Path key.

If you have more than one dependency jar files, you can put them
all but separated by a space, i.e.,:

Class-Path: /opt/http-client-3.0/http-client.jar log4j.jar myjar.jar etc.

Chris Sleep

Joined: Apr 05, 2006
Posts: 3
Thanks very much,

dropping the -jar and including my own jar on the classpath had completely failed to occur to me, and that does the trick perfectly.

I agree. Here's the link:
subject: Pain with classpaths..
It's not a secret anymore!