13

Okay, I'm using Ant version 1.7.1 (default install) on CentOS 6.3:

[theuser@dev-ingyhere ~]$ ant -version
Apache Ant version 1.7.1 compiled on August 24 2010
[theuser@dev-ingyhere ~]$ cat /etc/*-release
CentOS release 6.3 (Final)

I have JAVA_HOME set and I run ant:

[theuser@dev-ingyhere ~]$ export JAVA_HOME=/usr/java/jdk1.7.0_17 ; echo $JAVA_HOME ;
/usr/java/jdk1.7.0_17
[theuser@dev-ingyhere ~]$ ant -diagnostics | grep java\\.home
java.home : /usr/java/jdk1.7.0_17/jre

This is even more fun:

[theuser@dev-ingyhere ~]$ export JAVA_HOME=/a/fools/folly ; echo $JAVA_HOME ; ant -diagnostics | grep java\\.home
/a/fools/folly
java.home : /usr/java/jdk1.7.0_17/jre
[theuser@dev-ingyhere ~]$  env | grep JAVA
JAVA_HOME=/a/fools/folly 

So, I do get one thing -- apparently Oracle's Java 7 Javadoc for Class System is WRONG (aghast!) where it describes the java.home System Property as the "Java installation directory." I know that because the Java(TM) Tutorials for System Properties describes the java.home System Property as the "Installation directory for Java Runtime Environment (JRE)." In other words the JAVA_HOME in the environment does not necessarily equal java.home in the JVM System Properties. (What sets that?!)

QUESTION: Where and how does Ant get/set the system property java.home?

ingyhere
  • 11,818
  • 3
  • 38
  • 52
  • Well, the "java.home" value reported by Ant is actually directly from java.lang.System. The code starts on line 328 in the Diagnostics class of Apache Ant 1.7.1. So, this is really a Java question. – ingyhere May 14 '13 at 02:35
  • Did you find a workaround for this? – givanse Aug 09 '14 at 16:21
  • 3
    Yes, you can work around it by accessing the system environment property within Ant using ${env.JAVA_HOME}. Alternately, specify java in the Javac Task by setting the executable property to the javac path and the fork property to yes (see Ant's Javac Task Documentation). You can probably also change how JVMs are recognized in the system by following this guide, then Ant should launch with the proper JVM: http://www.if-not-true-then-false.com/2010/install-sun-oracle-java-jdk-jre-7-on-fedora-centos-red-hat-rhel/ . – ingyhere Aug 12 '14 at 02:38

2 Answers2

15

Really a JVM internals question

Since Ant is just echoing the java.lang.System properties (see comment above under original post), this is really a JVM question. The Java HotSpot Virtual Machine is the core interpreter. Code is available online at hg.openjdk.java.net.

On line 309 of the C++ code for HotSpot (os_linux.cpp) there is a an init_system_properties_values() method in the os class. It does some mild heuristics to kind of sniff out the location for a variable named home_path which ends up being set to what Java users see as "java.home". Comments in the code indicate that '<java_home>/jre' is being formally specified as the java.lang.System property value for "java.home" (in the case of a JDK install).

ingyhere
  • 11,818
  • 3
  • 38
  • 52
0

I had a JDK installed on a Windows box in d:\jdk but running its d:\jdk\bin\java.exe with -XshowSettings showed its built-in java.home pointing to the system-wide JRE install in c:\Program Files\Java\jre1.8.0_91. I suspect that I corrupted my install by deleting d:\jdk\jre without understanding the role of a private JRE vs public JRE.

eel ghEEz
  • 1,186
  • 11
  • 24
  • Java 8 installs a little differently than earlier versions. In order to exert more control over executables for security reasons, Oracle installs java.exe, javaws.exe and javaw.exe in the %WINDOWS%\system32 folder. Additionally, the path is hardcoded into a data file at C:\programdata\Oracle\Java\javapath. See the answer at http://stackoverflow.com/questions/26324486/properly-installing-java-8-along-with-java-7 for more info. So I don't really think this is an Ant-specific issue. – ingyhere Jun 21 '16 at 01:04
  • I did not rely on %PATH% to execute java.exe in my corrupt JDK install. It was either my deletion of d:\jdk\jre or installing the 64-bit JDK on top of the 32-bit one that caused d:\jdk\bin\java.exe look for the source of its home elsewhere (in the registry, I guess). – eel ghEEz Jun 21 '16 at 03:30