Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Ant set its 'java.home' (and is it wrong) and is it supposed to append '/jre'?

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?

like image 565
ingyhere Avatar asked May 14 '13 00:05

ingyhere


People also ask

Should I set JAVA_HOME to JDK or JRE?

If you're doing any sort of development, or building with Maven or Ant, you need to point to the JDK (Java Development Kit) where utilities such as javac (the Java Compiler) reside. Otherwise, you can point to the JRE (Java Runtime Environment).

Can we set JAVA_HOME to JRE?

Set the JAVA_HOME System Variable Click the Advanced tab, and then click Environment Variables. Under System Variables, look for the JAVA_HOME system variable. The JAVA_HOME path should point to the location that you recorded when you installed the JRE.

Where should JAVA_HOME be set to?

Set JAVA_HOME: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1. 6.0_02.

What happens if JAVA_HOME is not set?

If any program that requires a Java runtime fails to find the JAVA_HOME environment variable upon startup, or if the JAVA_HOME environment variable is misconfigured, it will result in some of the following error messages to be displayed: A Java installation exists but JAVA_HOME has been set incorrectly.


1 Answers

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

like image 69
ingyhere Avatar answered Oct 22 '22 03:10

ingyhere