Java Ant 无法启动,出现主类错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19497182/
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-08-12 17:45:53  来源:igfitidea点击:

Ant unable to launch, giving a main class error

javaant

提问by Noah

I'm running Elementary OS (Ubuntu 12 based), and I'm having issues running apache ant. It was working earlier before a restart, so I'm not sure what would've changed.

我正在运行 Elementary OS(基于 Ubuntu 12),并且在运行 apache ant 时遇到问题。它在重新启动之前工作得更早,所以我不确定会发生什么变化。

I've defined environment variables in /etc/environment as follows:

我在 /etc/environment 中定义了环境变量,如下所示:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
ANT_HOME="/opt/ant"
ANT_OPTS="-Xms256M -Xmx512M"

So my Java and Ant environment variables should be set. I'm trying to deploy with ant, with 'ant clean deploy', but I get an error in my terminal:

所以应该设置我的 Java 和 Ant 环境变量。我正在尝试使用 ant 进行部署,使用“ant clean deploy”,但我的终端出现错误:

Error: Could not find or load main class org.apache.tools.ant.launch.Launcher

I've tried "source /etc/environment". Running 'echo $ANT_HOME' shows the correct path. I've tried moving ant to a different location and resetting the variables. Nothing. I'm kind of lost. Please help!

我试过“源/等/环境”。运行 'echo $ANT_HOME' 显示正确的路径。我试过将 ant 移动到不同的位置并重置变量。没有。我有点失落。请帮忙!

回答by Frederic Close

you should define $ANT_HOME before using it in your $PATH

您应该在 $PATH 中使用它之前定义 $ANT_HOME

  JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
  ANT_HOME="/opt/ant"
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"

double check that you have ant-launcher.jar under $ANT_HOME/lib

仔细检查 $ANT_HOME/lib 下是否有 ant-launcher.jar

回答by David W.

As others have stated, it looks like ANT_HOMEisn't being set before you declare it in your path.

正如其他人所说,ANT_HOME在您在路径中声明之前似乎没有设置它。

I take a slightly different approach to these things. Instead of constantly updating my PATH, I usually set my path to something fairly simple:

我对这些事情采取了稍微不同的方法。我通常将路径设置为相当简单的内容,而不是不断更新我的 PATH:

 PATH="/usr/local/bin:/bin:/usr/bin/:$HOME/bin"

Then, I make hard links to various binaries into my /usr/local/bindirectory.

然后,我将各种二进制文件的硬链接添加到我的/usr/local/bin目录中。

$ cd $ANT_HOME/bin  # All the Ant binaries
> for binary in *
> do
> sudo ln -s $PWD/$binary /usr/local/bin
> done

Now, I don't have to add $ANT_HOME/binto my $PATH. Even better, if I include /usr/local/binbefore /usr/binand /bin, I am picking the binary in /usr/local/binfirst. This way, I can ensure I run the version I installed over the default. For example, my machine comes with Ant 1.7 in /usr/bin/antbut I want to use Ant 1.9.1. I can install Ant 1.9.1, and that will be my default version.

现在,我不必添加$ANT_HOME/bin到我的$PATH. 更好的是,如果我/usr/local/bin/usr/bin和之前包含/bin,我会/usr/local/bin首先选择二进制文件。这样,我可以确保运行我安装的版本而不是默认版本。例如,我的机器带有 Ant 1.7,/usr/bin/ant但我想使用 Ant 1.9.1。我可以安装 Ant 1.9.1,这将是我的默认版本。

回答by SKhalymon

I had same error when install ant with npm install. When I tried install from official repository throw pacman -S apache-ant(apt-get install apache-ant-- for Debian/Ubuntu) it's start working proper for me.

使用 npm install 安装 ant 时我遇到了同样的错误。当我尝试从官方存储库 throw 安装时pacman -S apache-antapt-get install apache-ant-- 对于 Debian/Ubuntu),它开始对我正常工作。

回答by Everlight

I'm experiencing this bug with jdk 1.8. But, I came across this RHEL 6.5 bug for OpenJDK 1.8 which may be related: https://bugzilla.redhat.com/show_bug.cgi?id=1149605

我在 jdk 1.8 中遇到了这个错误。但是,我遇到了 OpenJDK 1.8 的这个 RHEL 6.5 错误,这可能与此相关:https://bugzilla.redhat.com/show_bug.cgi?id =1149605

Essentially, it's fixed in jpackage-utils-1.7.5-3.13 which is included with RHEL 5.7.

本质上,它在 RHEL 5.7 中包含的 jpackage-utils-1.7.5-3.13 中得到了修复。

The problem, as Javier Ramirez said in the bug comments:

问题,正如哈维尔·拉米雷斯 (Javier Ramirez) 在错误评论中所说:

Your script /usr/share/java-utils/java-functionshas problems with "openjdk version" because it expects "java version" as Java 7 does.

您的脚本/usr/share/java-utils/java-functions在“openjdk 版本”方面存在问题,因为它像 Java 7 一样需要“java 版本”。

$ mkdir /usr/share/java-1.8.0
$ mkdir /usr/lib/java-1.8.0
$ diff /usr/share/java-utils/java-functions.orig /usr/share/java-utils/java-functions
149,150c149,150
<             -e '/java \(full \)*version "/s/'$re'/<<<>>>/' \
<             -e '/java \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*//p')
---
>             -e '/[java|openjdk] \(full \)*version "/s/'$re'/<<<>>>/' \
>             -e '/[java|openjdk] \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*//p')

------

回答by ron shenk

I am using RHEL which comes with ant (in /usr/bin/ant) and the ant libraries in /usr/share/ant. As suggest above, ant -version gave the "could not find" error. I installed my own version of ant (in /home/Ant since I have a lot of space in /home) and put ANT_HOME in my .bash_profile and $ANT_HOME/bin in my PATH and $ANT_HOME and $ANT_HOME/bin (for good measure) in my CLASSPATH. Then ant worked (when used as myself, not as root).

我使用的 RHEL 与 ant(在 /usr/bin/ant 中)和 /usr/share/ant 中的 ant 库一起使用。如上所述, ant -version 给出了“找不到”错误。我安装了我自己的 ant 版本(在 /home/Ant 中,因为我在 /home 中有很多空间)并将 ANT_HOME 放在我的 .bash_profile 中,将 $ANT_HOME/bin 放在我的 PATH 和 $ANT_HOME 和 $ANT_HOME/bin 中(永远测量)在我的 CLASSPATH 中。然后 ant 工作了(当我自己使用时,而不是 root 时)。