Ant flagrantly ignoring JAVA_HOME environment vari

2019-07-01 14:50发布

问题:

I just picked up the latest version of the Android SDK and started trying to use it. Unlike almost everyone else coming up with this problem, I'm running Linux, namely Linux Mint 13. I'm currently trying to compile a Hello World program using ant, installed via

sudo apt-get install ant

and ran in the project folder:

ant debug

However, it fails utterly to compile, eventually spitting out an error to do with setting JAVA_HOME. I amended my ~/.bashrc file accordingly and restarted, but I still get the error:

Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-7-openjdk-amd64/jre"

Total time: 1 second
jamie@jamie-ThinkPad-E525 ~/Downloads/adt-bundle-linux/sdk/tools/projects/new $ echo $JAVA_HOME
/usr/java/jdk1.7.0_05/

As you can see, it's lying through its teeth.

I've found lots of references to this problem, but most people have either set their JAVA_HOME incorrectly, or set it to the JRE. Clearly, I have done neither.

I also amended my project folder's ant.properties file, adding the line

java.home=/usr/java/jdk1.7.0_05/

to no avail.

Has anyone else experienced/solved this problem, or got any ideas? Thanks.

回答1:

The chances are that ant is telling the truth and that the environment variable is not set. The chances are that:

  • you put the wrong statement into the ".bashrc" file, or
  • you didn't restart properly.

Anyway, you can verify this by running export in the shell before to run the ant command ... and looking to see if the JAVA_HOME variable is listed.


Hints:

1) This is wrong:

JAVA_HOME=/usr/java/jdk1.7.0_05/

That only creates a local shell variable, and local shell variables are NOT passed to child process (like the ant command). It should be:

export JAVA_HOME=/usr/java/jdk1.7.0_05/

2) Try running this:

export JAVA_HOME=/usr/java/jdk1.7.0_05/
ant

3) Adding java.home=/usr/java/jdk1.7.0_05/ to ant.properties won't help. Ant expects the setting in an environment variable.

4) Computer programs don't lie. They tell the truth as they see it. Or to be more accurate, the whole notion of lying and telling the truth is meaningless unless the agent is capable of intention. But the point is that if you start to suspect computer programs of trying to deceive you, you are going to have a hard time debugging things.

(OK, you were joking. But many people faced with a troubleshooting problem take a similarly unproductive approach; e.g. assuming that every tricky Java problem is evidence that the compiler / language / runtime is broken. IMO - it is worth reminding people that this kind of thinking can be very unhelpful ....)



回答2:

Try:

$ ant -diagnostics | grep java\\.home

if it's not what you expect, then

$ JAVA_HOME=/path/to/jdk ant -diagnostics | grep java\\.home


回答3:

I have a server with ant 1.9.1. It needs an environment variable ANT_RESPECT_JAVA_HOME set, or it will flagrantly ignore the JAVA_HOME environment variable. that does not seem to be the case on another server with ant 1.9.6.

So, depending on you version of ant you might need to add

export ANT_RESPECT_JAVA_HOME=true

in addition to

export JAVA_HOME=/usr/java/jdk1.7.0_05/

Hope that this helps someone as it took me way too long to discover...