Link to home
Start Free TrialLog in
Avatar of grexx
grexx

asked on

[Mac OSX] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Main

I'm trying to build an example servlet from the FOP application (http://xml.apache.org/fop) on a Mac OS X 10.3. I've set the JAVA_HOME variable to /library/java/home. I've copied the Ant-directory to /library/java/extensions, where the Fop directory is as well. I've set the ANT_HOME variable to the proper folder as well, although I don't know if this is necessary. Now if I run build.sh, I get the following error.

Building with classpath /Library/Java/Home/lib/tools.jar:/Library/Java/Home/lib/classes.zip:../../lib/ ant-1.5.1.jar:../../lib/xml-apis.jar:../../lib/xercesImpl-2.2.1.jar:../../lib/xalan-2.4.1.jar
Starting Ant...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Main

I suppose this has to do with Ant, or with the classpath variable. If I run "env" in the terminal, I don't see a classpath variable. Furthermore, I've installed Ant 1.6.2 and I see a reference to 1.5.1, so that could be the problem?!

I've found the following tip to set the classpath (%SET CLASSPATH = ${CLASSPATH}:.), but when I run it, it says "undefined variable". If I use a hard path, it says "too many arguments".

Then I'm not sure what path I should use?! Should I use the lib-folder of Ant? I have Xerces, Fop and Xalan as well, do I add those too?
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grexx
grexx

ASKER

I think I've found the problem. In build.sh I see the following line:

LOCALCLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/classes.zip

If I look for these files (tools.jar and classes.zip), they are not present. The file list for /library/java/home/lib is:

Xusage.txt
audio/
client_jvm.cfg
cmm/
content-types.properties
dt.jar
endorsed/
ext/
flavormap.properties
fonts/
im/
images/
ir.idl
jaws.jar
jce.jar
jvm.cfg
jvm.hprof.txt
logging.properties
netscape.jar
orb.idl
psfont.properties.ja
psfontj2d.properties
security/
server_jvm.cfg
swing.properties
tzmappings
zi/
$JAVA_HOME/lib/classes.zip belongs to java < 1.3
If you have Java > 1.3 (as it seems) then it is called rt.jar (or dt.jar) and there is no need to include it in the classpath.

$JAVA_HOME/lib/tools.jar should be found directly under $JAVA_HOME/lib (but not having it should not cause this problem).

Are you sure that $ANT_HOME is set correctly and that $ANT_HOME/bin is in your PATH?

What is build.sh and how do you invoke ant?
Avatar of grexx

ASKER

Build.sh runs a build.xml script (using Ant) to compile a servlet. The servlet is in the examples folder of the FOP application.

In build.sh, I removed the lines that referred to classes.zip and tools.jar. Then I've changed the classpath using JAVA_EXT as you can see below. If I run build.sh (in the terminal in the directory itself: ./build.sh), it gives the following error.

% ./build.sh

Fop Build System
----------------

Ant home: /Library/Java/Extensions/apache-ant-1.6.2
Java home: /Library/Java/Home

Building with classpath :
/Library/Java/Extensions/apache-ant-1.6.2/lib/ant.jar:
/Library/Java/Extensions/fop-0.20.5/lib/xml-apis.jar:
/Library/Java/Extensions/fop-0.20.5/lib/xercesImpl-2.2.1.jar:
/Library/Java/Extensions/fop-0.20.5/lib/xalan-2.4.1.jar
Starting Ant...

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

===============================================
Below the contents of the build.sh file
===============================================

echo Ant home: $ANT_HOME
echo Java home: $JAVA_HOME

JAVA_EXT=/Library/Java/Extensions
# ANT_HOME=/Library/Java/Extensions/apache-ant-1.6.2

# LOCALCLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/classes.zip
LOCALCLASSPATH=$LOCALCLASSPATH:$ANT_HOME/lib/ant.jar
LOCALCLASSPATH=$LOCALCLASSPATH:$JAVA_EXT/fop-0.20.5/lib/xml-apis.jar
LOCALCLASSPATH=$LOCALCLASSPATH:$JAVA_EXT/fop-0.20.5/lib/xercesImpl-2.2.1.jar
LOCALCLASSPATH=$LOCALCLASSPATH:$JAVA_EXT/fop-0.20.5/lib/xalan-2.4.1.jar

echo Building with classpath $LOCALCLASSPATH
echo Starting Ant...

$JAVA_HOME/bin/java -Dant.home=$ANT_HOME -classpath "$LOCALCLASSPATH" org.apache.tools.ant.Main $*
The missing jar is ant-launcher.jar (should be found at $ANT_HOME/lib/) which you will need to include in your CLASSPATH.
But why are you launching ant this way and not using one of its scripts? (as I specfied before).
Once you get over this missing class problem (by adding $ANT_HOME/lib/ant-launcher.jar to your classpath) you might find other classes
missing (from $ANT_HOME/lib/*.jar)
Avatar of grexx

ASKER

Why am I launching Ant this way? Compiling stuff like this is not my daily job. I have done things like this before, although I can't remember how I did it then. I believe in the manual of fop it said something about the build.sh file, so I tried to run it. That's the reason. But I suppose you say there is a better way to do this?
Yes, though the fix I provided you above should solve that problem.
Ant comes with scripts to run it (under $ANT_HOME/bin).
After setting your classpath (in your build.sh) invoke of one of those scripts instead of
runing ant it directly via java.
Avatar of grexx

ASKER

I finally got it. I used $ANT_HOME/bin/ant -f /{servlet-location}/build.xml. Thanks!
:-)