4

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.

Qualia
  • 700
  • 8
  • 15

3 Answers3

5

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Huh, you know what, I was sure I'd set it using `export JAVA_HOME...`, but on checking, I had actually just set it locally. Thanks for making me check again. I was, of course, joking when I said it was lying. I more assumed that it was doing something wrong. – Qualia Dec 16 '12 at 23:29
  • 1
    What about: Ant is telling the truth and the environment variable IS set. http://stackoverflow.com/questions/16533256/where-does-ant-set-its-java-home-and-is-it-wrong-and-is-it-supposed-to-appen – ingyhere Oct 21 '13 at 20:59
  • Setting an environment variable in the wrong place (i.e. where Ant can't see it) and not setting it are ... as far as Ant is concerned ... indistinguishable. When I said *"the environment variable is not set"* ... I obviously meant, that it is not set in the place where Ant looks for it; i.e. in *its* environment. – Stephen C Aug 09 '14 at 16:37
  • The problem with environment variables is that lots of people simply don't understand how they work. – Stephen C Aug 09 '14 at 16:38
0

Try:

$ ant -diagnostics | grep java\\.home

if it's not what you expect, then

$ JAVA_HOME=/path/to/jdk ant -diagnostics | grep java\\.home
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 2
    Ant reports the java.lang.System property for its 'java.home'. This isn't the same as 'JAVA_HOME' set in the environment. The Oracle HotSpot code adds '/jre' to the end of the 'java.home' path by design. – ingyhere May 14 '13 at 04:09
0

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

dougwm
  • 1
  • 1