Ant unable to launch, giving a main class error

偶尔善良 提交于 2021-02-09 19:04:33

问题


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.

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

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:

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!


回答1:


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

  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




回答2:


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

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="/usr/local/bin:/bin:/usr/bin/:$HOME/bin"

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

$ 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/bin to my $PATH. Even better, if I include /usr/local/bin before /usr/bin and /bin, I am picking the binary in /usr/local/bin first. 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/ant but I want to use Ant 1.9.1. I can install Ant 1.9.1, and that will be my default version.




回答3:


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.




回答4:


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

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

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

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

$ 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'/<<<\1>>>/' \
<             -e '/java \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')
---
>             -e '/[java|openjdk] \(full \)*version "/s/'$re'/<<<\1>>>/' \
>             -e '/[java|openjdk] \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')

------



回答5:


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).



来源:https://stackoverflow.com/questions/19497182/ant-unable-to-launch-giving-a-main-class-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!