Homunculus Reticulli Homunculus Reticulli - 3 months ago 6
Java Question

Exception in thread "main" java.lang.NoClassDefFoundError

I am getting an exception exception when attempting to run a Java sample application.

Below is a stack trace from the console:

$./init-og-examples-db.sh
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Task
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Task
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Task
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Task
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
### Adding example data
Error: Could not find or load main class com.opengamma.examples.tool.ExampleDatabasePopulator
### Completed


I am relatively new to Java, so I am not sure why the class is not been found, since the class does exist:

someone@yourbox:~/work/dev/java/OG-Platform/projects/OG-Examples/scripts$ find ../../.. -type f -name 'ExampleDatabasePopulator.*'
../../../projects/OG-Examples/bin/com/opengamma/examples/tool/ExampleDatabasePopulator.class
../../../projects/OG-Examples/build/classes/com/opengamma/examples/tool/ExampleDatabasePopulator.class
../../../projects/OG-Examples/src/main/java/com/opengamma/examples/tool/ExampleDatabasePopulator.java
../../../projects/OG-BloombergExample/bin/com/opengamma/bloombergexample/tool/ExampleDatabasePopulator.class
../../../projects/OG-BloombergExample/build/classes/com/opengamma/bloombergexample/tool/ExampleDatabasePopulator.class
../../../projects/OG-BloombergExample/src/main/java/com/opengamma/bloombergexample/tool/ExampleDatabasePopulator.java


Can anyone help me fix this?

[Update]

I have done the following:

1. Removed and manually installed ant on my machine

2. Searched for .jar files containing the word Task (in an attempt to find the file that implements the Task class)

grep -lri Task --include=*.jar /usr/local/apache-ant-1.9.1
/usr/local/apache-ant-1.9.1/lib/ant-netrexx.jar
/usr/local/apache-ant-1.9.1/lib/ant-jdepend.jar
/usr/local/apache-ant-1.9.1/lib/ant-javamail.jar
/usr/local/apache-ant-1.9.1/lib/ant-junit.jar
/usr/local/apache-ant-1.9.1/lib/ant-jsch.jar
/usr/local/apache-ant-1.9.1/lib/ant-apache-xalan2.jar
/usr/local/apache-ant-1.9.1/lib/ant-junit4.jar
/usr/local/apache-ant-1.9.1/lib/ant-jmf.jar
/usr/local/apache-ant-1.9.1/lib/ant.jar
/usr/local/apache-ant-1.9.1/lib/ant-testutil.jar
/usr/local/apache-ant-1.9.1/lib/ant-jai.jar
/usr/local/apache-ant-1.9.1/lib/ant-commons-net.jar
/usr/local/apache-ant-1.9.1/lib/ant-antlr.jar
/usr/local/apache-ant-1.9.1/lib/ant-swing.jar


3. Added the relevant folders to my CLASSPATH


export
CLASSPATH=$CLASSPATH:/usr/local/apache-ant-1.9.1/etc:/usr/local/apache-ant-1.9.1/lib


4. Made sure that I have both
JAVA_HOME
and
ANT_HOME
set correctly

echo $ANT_HOME
/usr/local/apache-ant-1.9.1
echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle/jre


Still, I get exactly the same errors as before (i.e. stack trace is same as that above).

[2nd Update]

This is the contents of the script file I am running:

#!/bin/sh

if [ ! -z "$JAVA_HOME" ]; then
JAVA=$JAVA_HOME/bin/java
elif [ -x /opt/jdk1.6.0_16/bin/java ]; then
JAVA=/opt/jdk1.6.0_16/bin/java
else
# No JAVA_HOME, try to find java in the path
JAVA=`which java 2>/dev/null`
if [ ! -x "$JAVA" ]; then
# No java executable in the path either
echo "Error: Cannot find a JRE or JDK. Please set JAVA_HOME"
exit 1
fi
fi

if [ "`basename $0`" = "init-og-examples-db.sh" ] ; then
cd `dirname $0`/.. #PLAT-1527
fi

CLASSPATH=config:og-examples.jar
for FILE in `ls -1 lib/*` ; do
CLASSPATH=$CLASSPATH:$FILE
done

echo "### Creating empty database"

$JAVA -cp "$CLASSPATH" \
-Dlogback.configurationFile=jetty-logback.xml \
com.opengamma.util.test.DbTool \
-jdbcUrl jdbc:hsqldb:file:install/db/hsqldb/example-db \
-database og-financial \
-user "OpenGamma" \
-password "OpenGamma" \
-drop true \
-create true \
-createtables true \
-dbscriptbasedir .

$JAVA -cp "$CLASSPATH" \
-Dlogback.configurationFile=jetty-logback.xml \
com.opengamma.util.test.DbTool \
-jdbcUrl jdbc:hsqldb:file:temp/hsqldb/og-fin-user \
-database og-financial \
-user "OpenGamma" \
-password "OpenGamma" \
-drop true \
-create true \
-createtables true \
-dbscriptbasedir .

echo "### Adding example data"

$JAVA -cp "$CLASSPATH" \
-Xms512M \
-Xmx1024M \
-Dlogback.configurationFile=jetty-logback.xml \
com.opengamma.examples.tool.ExampleDatabasePopulator

echo "### Completed"

Answer

Please note that your CLASSPATH is not valid for your execution. Your CLASSPATH must be

export CLASSPATH=$CLASSPATH:/usr/local/apache-ant-1.9.1/etc:/usr/local/apache-ant-1.9.1/lib/ant-netrexx.jar:/usr/local/apache-ant-1.9.1/lib/ant-jdepend.jar:/usr/local/apache-ant-1.9.1/lib/ant-javamail.jar:/usr/local/apache-ant-1.9.1/lib/ant-junit.jar:/usr/local/apache-ant-1.9.1/lib/ant-jsch.jar:/usr/local/apache-ant-1.9.1/lib/ant-apache-xalan2.jar:/usr/local/apache-ant-1.9.1/lib/ant-junit4.jar:/usr/local/apache-ant-1.9.1/lib/ant-jmf.jar:/usr/local/apache-ant-1.9.1/lib/ant.jar:/usr/local/apache-ant-1.9.1/lib/ant-testutil.jar:/usr/local/apache-ant-1.9.1/lib/ant-jai.jar:/usr/local/apache-ant-1.9.1/lib/ant-commons-net.jar:/usr/local/apache-ant-1.9.1/lib/ant-antlr.jar:/usr/local/apache-ant-1.9.1/lib/ant-swing.jar

If you name only a directory only the .class files from the directory will be included.

.jar files must be named explicitly in the CLASSPATH as they are not included automatically.