尝试运行 Java 示例应用程序时出现异常异常。
下面是来自控制台的堆栈跟踪:
$./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
我对 Java 比较陌生,所以我不确定为什么找不到该类,因为该类确实存在:
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
谁能帮我解决这个问题?
[更新]
我做了以下事情:
1.在我的机器上删除并手动安装ant
2.搜索包含单词Task的.jar文件(试图找到实现Task类的文件)
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.将相关文件夹添加到我的CLASSPATH
导出 CLASSPATH=$CLASSPATH:/usr/local/apache-ant-1.9.1/etc:/usr/local/apache-ant-1.9.1/lib
4.确保我拥有两者JAVA_HOME
并ANT_HOME
正确设置
echo $ANT_HOME
/usr/local/apache-ant-1.9.1
echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle/jre
尽管如此,我还是得到了与以前完全相同的错误(即堆栈跟踪与上面的相同)。
[第二次更新]
这是我正在运行的脚本文件的内容:
#!/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"