Bug 52632

Summary: JAVA_HOME incorrectly defined in ant shell script for Mac OS X 10.7 (Lion)
Product: Ant Reporter: ashirazi
Component: Wrapper scriptsAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 1.8.2   
Target Milestone: 1.9.0   
Hardware: Macintosh   
OS: other   

Description ashirazi 2012-02-09 17:12:39 UTC
On Mac OS X 10.7 (Lion), running the default ant command (with no shell ANT_HOME set) gives me:
$ ant
Error: JAVA_HOME is not defined correctly.
  We cannot execute /System/Library/Frameworks/JavaVM.framework/Home/bin/java


Below is a patch that fixes the problem. Your ant shell script correctly calculates JAVACMD in the following line:
    JAVACMD=`which java 2> /dev/null `
But was unable to get to this point because the JAVA_HOME had been incorrectly hard-coded for Darwin (Mac OS X).

--- /opt/local/share/java/apache-ant/bin/ant.orig	2012-02-09 12:02:30.000000000 -0500
+++ /opt/local/share/java/apache-ant/bin/ant	2012-02-09 12:02:53.000000000 -0500
@@ -83,11 +83,7 @@
 mingw=false;
 case "`uname`" in
   CYGWIN*) cygwin=true ;;
-  Darwin*) darwin=true
-           if [ -z "$JAVA_HOME" ] ; then
-             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
-           fi
-           ;;
+  Darwin*) darwin=true ;;
   MINGW*) mingw=true ;;
 esac
Comment 1 ashirazi 2012-02-09 19:02:51 UTC
If the ant script must set a JAVA_HOME, I would suggest basing it on the calculated JAVACMD. For example:

if [ -L "$JAVACMD" ]; then JAVA_HOME=`readlink "$JAVACMD"`/../..; else JAVA_HOME="$JAVA_CMD"/../..; fi
Comment 2 Stefan Bodewig 2012-02-12 07:27:46 UTC
Actually JAVA_HOME on a Mac is set mostly for jikes later down inside the script (just look for the only other place that references darwin).  I don't think simply going two levels up from wherever java is locates is going to help, maybe we just don't set JAVA_HOME unless there is an executable java where we expect to find it as well.

Where is the java executable in Lion (and where would we find the runtime libs that used to be in /System/Library/Frameworks/JavaVM.framework/Classes)?
Comment 3 Stefan Bodewig 2012-02-21 15:11:46 UTC
Just checked with a co-worker who is using Lion: /System/Library/Frameworks/JavaVM.framework/Home exists, as does /System/Library/Frameworks/JavaVM.framework/Home/bin/java and the later is executable.  I'm puzzled.

As for your suggested workaround, I'd rather check whether /System/Library/Frameworks/JavaVM.framework/Home/bin/java is executable before setting JAVA_HOME and not set it at all otherwise.
Comment 5 ashirazi 2012-08-24 18:51:47 UTC
Keep in mind that you want to configure JAVA_HOME and JAVACMD using the same mechanisms.

JAVA_HOME="`/usr/libexec/java_home`"
JAVACMD="$JAVA_HOME/bin/java"

or

JAVACMD="`which java 2> /dev/null `"
JAVA_HOME="$JAVACMD/../.."
# Note I have left out logic to identify and follow symlinks here

The point is they should be consistent, since the java configured on the path may be different from that configured OS-wide.
Comment 6 ashirazi 2012-08-24 18:56:01 UTC
Oh, and for the record:
$ /usr/libexec/java_home                                          
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

This is different from what your colleague has configured.

All the more reason to try to derive JAVA_HOME/JAVACMD from a known executable (preferable the one on the PATH).
Comment 7 Stefan Bodewig 2013-01-17 14:18:59 UTC
I've ported Tomcat's look up algorithm with svn revision 1434680