15

When trying to compile a Javadoc taglet, which requires $JAVA_HOME/lib/tools.jar, I discovered that ant (version 1.8.4) sets java.home to $JAVA_HOME/jre rather than just $JAVA_HOME. I verified this thusly:

<echo>${java.home}</echo>
<echo>${env.JAVA_HOME}</echo>

[echo] /usr/java/jdk1.7.0_21/jre
[echo] /usr/java/jdk1.7.0_21

According to ant -diagnostics, there isn't any property like a jdk.home. Thus, to use tools.jar I have to do:

<classpath location="${java.home}/../lib/tools.jar"/>

So, I have two questions:

1) Is there something wrong with my setup of ant that's causing java.home to point to the JRE instead of the JDK?

2) If this is the way ant is supposed to work, is using the .. in my classpath the way I'm supposed to do things? Or should I do ${env.JAVA_HOME}/lib/tools.jar? Or something else entirely?

Matthew Cline
  • 2,312
  • 1
  • 19
  • 36
  • ant is a shell script. Try running it with shell debug on (+x). Ensure that java found by ant is real java (not any soft links) – Jayan Jul 04 '13 at 15:33
  • 1) I edited the shell script to put `echo $JAVA_HOME` right before the last line, and it was at that point unchanged. 2) I tried renaming the JDK directory and putting a symlink to the new name in its place. `${java.home}` changed to reflect the renamed directory, so it's using the right directory. – Matthew Cline Jul 04 '13 at 21:14
  • Sorry I didn't see this until now! – ingyhere Jul 24 '13 at 23:27
  • possible duplicate of [Ant flagrantly ignoring JAVA\_HOME environment variable](http://stackoverflow.com/questions/13888163/ant-flagrantly-ignoring-java-home-environment-variable) – givanse Aug 09 '14 at 16:38

2 Answers2

7

Here are the answers:

  1. "Is there something wrong with my setup ...?" No. Ant is setting it's internal java.home based on JVM System properties. The code for HotSpot (JVM internals) sets it with "/jre" appended on purpose. In fact, the Java(TM) Tutorials for System Properties describes it exactly that way. The "java.home" variable from inside ant really isn't one-in-the-same as the "JAVA_HOME" that is set in your environment -- different but with similar names.

  2. "(What is) the way I'm supposed to do things?" You can really do whatever you feel is appropriate, but remember that Ant can and usually does run in a separate JVM process. I'd assume that your system environment is probably specifying the JVM that was used to develop the app, so I would just use "${env.JAVA_HOME}" to ensure that development expectations meet build expectations.

For more information, please see another similar answer here.

Also, consider that more info can be collected from ant by running it with the -debug, -diagnostics and/or -verbose flags.

Community
  • 1
  • 1
ingyhere
  • 11,818
  • 3
  • 38
  • 52
5

Had the same problem. I found that adding fork="true" to the javac tag solves this issue. So do something like this:

<javac target="1.7" source="1.7" fork="true" ...>

I will thank whoever can explain why this works.

grebulon
  • 7,697
  • 5
  • 42
  • 66
  • Your environment probable sets $JAVA_HOME as the JDK path. [Ant documentation](https://ant.apache.org/manual/Tasks/javac.html) states (regarding `fork=true`), "Defaults to the compiler of the Java version that is currently running Ant." Therefore, the Ant script itself does not set the wrong path to the JDK -- You'd really have to change the `javac` behavior of `fork` to do that. The jars are found in the JDK where they are supposed to be located. – ingyhere Nov 23 '15 at 19:23
  • 1
    I looked for a solution for 3 hours. I finally found it. I've added (fork="true") and solved my all problems. For sharing so many thankfuls. – NovaYear Dec 08 '16 at 20:19
  • THIS IS THE ANSWER. ant was snooping for the jre/ folder in the JAVA_HOME setting that was pointed to the JDK. Proved it by renaming jre to jre-bak and suddenly ant would settle for using the bin dir instead of the jre/bin dir, and would find javac and so successfully build. But the correct fix is fork=true, solved my problem, and I renamed jre-bak to jre again, no more trouble. – MAXdB Nov 10 '22 at 22:00