jQuery in Action, 2nd edition*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pain with classpaths.." Watch "Pain with classpaths.." New topic
Author

Pain with classpaths..

Chris Sleep
Greenhorn

Joined: Apr 05, 2006
Posts: 3
Hi

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:

#!/bin/sh

/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.
3.jar:/usr/local/classes/hibernate/commons-collections-2.1.1.jar:/usr/local/clas
ses/hibernate/commons-logging-1.0.4.jar:/usr/local/classes/hibernate/dom4j-1.6.1
.jar:/usr/local/classes/hibernate/hibernate3.jar:/usr/local/classes/hibernate/jt
a.jar:/usr/local/classes/hibernate/jtds.jar:/usr/local/classes/hibernate/log4j-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"/>
</fileset>
</path>

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

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.

Thanks!
Eduardo Dela Rosa
Greenhorn

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 ]

Eduardo dela Rosa<br />SCJP 1.4
Chris Sleep
Greenhorn

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Greenhorn

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:

<snippet>

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

</snippet>

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.

HTH.
Chris Sleep
Greenhorn

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.

cheers,
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pain with classpaths..
 
Similar Threads
NoClassDefFoundError
Problem with Ant Build
Could not parse configuration: /hibernate.cfg.xml
could not find the main class
AndroMDA using Ant