Ant 失败:线程“main”中的异常 java.lang.NoClassDefFoundError org/apache/tools/ant/launch/Launcher

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15134656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:36:24  来源:igfitidea点击:

Ant failure: Exception in thread “main” java.lang.NoClassDefFoundError org/apache/tools/ant/launch/Launcher

javalinuxantfedoraopenjdk

提问by Ameer Jewdaki

I'm on Fedora 17. I am trying to compile a project with an ant build file which is not compatible with Java 7. So I decided to install OpenJDK 6. JDK 6 is unfortunately removed from yum repositories, and I figured it would be easy to install it manually. I learned that there is no JAVA_HOME variable and alternatives system is used instead. So I downloaded OpenJDK binaries (from OSG if it matters) and installed java & javac & javaws using alternatives --installcommand. Checking java --versionand javac --versionproved it to be successful. But the strange thing happened is that ant is not working any more! When I type ant --execdebugI receive this message:

我在 Fedora 17 上。我正在尝试使用与 Java 7 不兼容的 ant 构建文件编译一个项目。所以我决定安装 OpenJDK 6。不幸的是,JDK 6 已从 yum 存储库中删除,我认为它会是易于手动安装。我了解到没有 JAVA_HOME 变量,而是使用了替代系统。所以我下载了 OpenJDK 二进制文件(如果重要的话,从 OSG 下载)并使用alternatives --install命令安装 java & javac & javaws 。检查java --versionjavac --version证明它是成功的。但奇怪的事情发生了,蚂蚁不再工作了!当我输入时,ant --execdebug我收到此消息:

exec "/usr/lib/jvm/openjdk-6.0.24/bin/java" -classpath "/usr/bin/build-classpath: 
error: JVM_LIBDIR /usr/lib/jvm-exports/openjdk-6.0.24 does not exist or is not a 
directory:/usr/bin/build-classpath: error: JVM_LIBDIR /usr/lib/jvm-exports/openjdk-6.0.24 
does not exist or is not a directory:/usr/lib/jvm/openjdk-6.0.24/lib/tools.jar"
-Dant.home="/usr/share/ant" -Dant.library.dir="/usr/share/ant/lib" 
org.apache.tools.ant.launch.Launcher -cp ""
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/Launcher
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.Launcher
    at java.net.URLClassLoader.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: org.apache.tools.ant.launch.Launcher. Program will exit.

As the case with java, there is no ANT_HOME variable too. But there wasn't an ANT_HOME even before installing java 6 while it was working fine. Ant is installed under /usr/bin/ant that's already in PATH.

与 java 的情况一样,也没有 ANT_HOME 变量。但是,即使在安装 java 6 之前也没有 ANT_HOME,但它运行良好。Ant 安装在已经在 PATH 中的 /usr/bin/ant 下。

UPDATE:I know there are similar questions qustion 1, question 2. But neither resolved my problem. For example adding ANT_HOME=/usr/bin deteriorates the situation, in that ant is not working even with JDK 7! Due to my unfamiliarity with alternatives system, I totally removed java related alternatives, added JAVA_HOME, and modified PATH instead. Again, java & javac are working fine, with java --version returning the right version, but ant is returning the same error. As the --execdebugtells us, JVM_LIBDIR is not pointing to the right location. I suppose that the value is set in java.conf file, so I'm pasting it here (I've installed JDK 6 & 7 on /usr/lib/jvm):

更新:我知道有类似的问题qustion 1question 2。但都没有解决我的问题。例如,添加 ANT_HOME=/usr/bin 会使情况恶化,因为即使使用 JDK 7 ant 也无法工作!由于我对替代系统不熟悉,我完全删除了与java相关的替代方案,添加了JAVA_HOME,并修改了PATH。同样,java 和 javac 工作正常,java --version 返回正确的版本,但 ant 返回相同的错误。正如--execdebug告诉我们的那样,JVM_LIBDIR 没有指向正确的位置。我想这个值是在 java.conf 文件中设置的,所以我把它粘贴在这里(我已经在 /usr/lib/jvm 上安装了 JDK 6 & 7):

# System-wide Java configuration file                                -*- sh -*-
#
# JPackage Project <http://www.jpackage.org/>

# Location of jar files on the system
JAVA_LIBDIR=/usr/share/java

# Location of arch-specific jar files on the system
JNI_LIBDIR=/usr/lib64/java

# Location for noarch jar files using arch-specifics jar files
JAVAJNI_LIBDIR=/usr/share/java-jni

# Root of all JVM installations
JVM_ROOT=/usr/lib/jvm

# You can define a system-wide JVM root here if you're not using the
# default one.
#
# If you have the a base JRE package installed
# (e.g. java-1.6.0-openjdk):
#JAVA_HOME=$JVM_ROOT/jre
#
# If you have the a devel JDK package installed
# (e.g. java-1.6.0-openjdk-devel):
#JAVA_HOME=$JVM_ROOT/java-1.7.0-openjdk-1.7.0.9.x86_64
JAVA_HOME=$JVM_ROOT/openjdk-6.0.24

# Options to pass to the java interpreter
JAVACMD_OPTS=

By just commenting and uncommenting the corresponding lines in java.conf, it turns that ant is working fine with 7 and fails with 6. when I echo JVM_LIBDIR just before javacommand in the ant shell, I receive user/share/javaas stated in java.conf. But ant --execdebughas returned something strange: JVM_LIBDIR /usr/lib/jvm-exports/openjdk-6.0.24. What is that and how can I fix it?

通过仅对 java.conf 中的相应行进行注释和取消注释,它会发现 ant 在 7 时运行良好,而在 6 时失败。当我java在 ant shell 中的命令之前回显 JVM_LIBDIR 时,我收到user/share/java了 java.conf 中所述的信息。但是ant --execdebug返回了一些奇怪的东西:JVM_LIBDIR /usr/lib/jvm-exports/openjdk-6.0.24。那是什么,我该如何解决?

If you could not tell the problem here, what is the neatest and safest way to install multiple versions of JDK, that can be switched easily?

如果您无法在这里说出问题,那么安装多个版本的JDK最简洁最安全的方法是什么,可以轻松切换?

Any help is well appreciated.

任何帮助都非常感谢。

回答by

You are missing a jar on the classpath. Make sure you have all the required libraries in the lib folder from which ant is picking up the dependencies.

您在类路径上缺少一个 jar。确保在 ant 从中获取依赖项的 lib 文件夹中拥有所有必需的库。

Check this similar question.

检查这个类似的问题。

回答by coolcfan

I can only answer the last question.

我只能回答最后一个问题。

WE DO NOT USE THE ALTERNATIVE SYSTEM, instead, we manage it manually.

我们不使用替代系统,而是手动管理它。

What we do in both Ubuntu and CentOS is that we always use the tar.gz from Oracle JDK website, extract it to a subfolder in /opt (like /opt/jdk_1.7.0_09), make a symbol link to the folder (like /opt/default_jdk -> /opt/jdk_1.7.0_09), and set the environment varibles where we point JAVA_HOME to the symbol link rather than the actual folder (JAVA_HOME=/opt/default_jdk).

我们在 Ubuntu 和 CentOS 中所做的是我们总是使用 Oracle JDK 网站上的 tar.gz,将其解压缩到 /opt 中的子文件夹(如/opt/jdk_1.7.0_09),创建指向该文件夹的符号链接(如/opt/default_jdk -> /opt/jdk_1.7.0_09),并设置环境变量我们将 JAVA_HOME 指向符号链接而不是实际文件夹 ( JAVA_HOME=/opt/default_jdk)。

In this way, if we want to change or update the jdk (e.g. from JDK 7 upadte 9 to JDK 7 upadte 15), we just download the tar.gz package, extract it to another folder (e.g. /opt/jdk_1.7.0_15), delete the old symbol link (which points to /opt/jdk_1.7.0_09) and create a new one pointing to the new folder (e.g. /opt/default_jdk -> /opt/jdk_1.7.0_15).

这样,如果我们要更改或更新jdk(例如从JDK 7 upadte 9 到JDK 7 upadte 15),我们只需下载tar.gz 包,将其解压到另一个文件夹(例如/opt/jdk_1.7.0_15),删除旧符号链接(指向/opt/jdk_1.7.0_09)并创建一个指向新文件夹的新文件夹(例如/opt/default_jdk -> /opt/jdk_1.7.0_15)。

Same way works with 32 and 64 bit jdks, different versions of Ant, Maven, Gradle, etc.

同样的方式适用于 32 位和 64 位 jdks、不同版本的 Ant、Maven、Gradle 等。

回答by Navin Mishra

Looks like something is wrong with your Java installation. Refer this http://www.linuxforums.org/forum/red-hat-fedora-linux/151698-problems-tomcat.htmlwith a similar issue which got resolved later.

您的 Java 安装似乎有问题。参考这个http://www.linuxforums.org/forum/red-hat-fedora-linux/151698-problems-tomcat.html有一个类似的问题,后来得到解决。