Maven
  1. Maven
  2. MNG-4226

Better detection of JAVA_HOME on Apple Mac OS X

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.2.2
    • Component/s: Command Line
    • Labels:
      None
    • Complexity:
      Intermediate
    • Number of attachments :
      2

      Description

      On mac JAVA_HOME is detected by using the following code:

                 if [ -z "$JAVA_VERSION" ] ; then
                   JAVA_VERSION="CurrentJDK"
                 else
                   echo "Using Java version: $JAVA_VERSION"
                 fi
                 if [ -z "$JAVA_HOME" ] ; then
                   JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
                 fi
      

      But this does not work in collaboration with Using "Java preferences" to change the actual java version to use as "CurrentJDK" does not change once you update the "java applications" order.

      There is an alternative (at least on Leopard) for determining current java home that is based on Java Preferences by using an apple provided script. So, as a replacement fo rthe code above the following could be used.

                 if [ -z "$JAVA_HOME" ] ; then
                   JAVA_HOME=`/usr/libexec/java_home | tail -1`
                 fi
      

      Could also be taht this is teh first attempt and if fails use the current way of determining home.

        Issue Links

          Activity

          Hide
          Hans Aikema added a comment -

          This is not fixed, or broken again. On my MacPro running Mavericks:

          abu:bin aikebah$ java -version
          java version "1.8.0"
          Java(TM) SE Runtime Environment (build 1.8.0-b132)
          Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
          abu:bin aikebah$ ./mvn -version
          Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T15:51:42+02:00)
          Maven home: /Users/aikebah/Documents/Apps/apache-maven-3.2.2
          Java version: 1.6.0_65, vendor: Apple Inc.
          Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
          Default locale: en_US, platform encoding: MacRoman
          OS name: "mac os x", version: "10.9.4", arch: "x86_64", family: "mac"
          abu:bin aikebah$

          Show
          Hans Aikema added a comment - This is not fixed, or broken again. On my MacPro running Mavericks: abu:bin aikebah$ java -version java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode) abu:bin aikebah$ ./mvn -version Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T15:51:42+02:00) Maven home: /Users/aikebah/Documents/Apps/apache-maven-3.2.2 Java version: 1.6.0_65, vendor: Apple Inc. Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home Default locale: en_US, platform encoding: MacRoman OS name: "mac os x", version: "10.9.4", arch: "x86_64", family: "mac" abu:bin aikebah$
          Hide
          Hans Aikema added a comment -

          Forgot to include the results of java_home:

          abu:bin aikebah$ /usr/libexec/java_home
          /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home

          Show
          Hans Aikema added a comment - Forgot to include the results of java_home: abu:bin aikebah$ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
          Hide
          Ian Springer added a comment -

          When you run "java -version", the shell is going to run the first java executable it finds in your PATH, which may not be the same as the java the mvn shell script uses, which is:

          1) if JAVA_HOME defined: $JAVA_HOME/bin/java
          2) else if /System/Library/Java/JavaVirtualMachines/CurrentJDK exists: /System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home/bin/java

          What is your PATH set to? You can also try running "whereis java".

          Show
          Ian Springer added a comment - When you run "java -version", the shell is going to run the first java executable it finds in your PATH, which may not be the same as the java the mvn shell script uses, which is: 1) if JAVA_HOME defined: $JAVA_HOME/bin/java 2) else if /System/Library/Java/JavaVirtualMachines/CurrentJDK exists: /System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home/bin/java What is your PATH set to? You can also try running "whereis java".
          Hide
          Hans Aikema added a comment -

          abu:classes aikebah$ whereis java
          /usr/bin/java

          And as mentioned earlier in this issue: on modern Mac OS the location of java should be examined using
          1) if JAVA_HOME defined: $JAVA_HOME/bin/java
          2) else if /usr/libexec/java_home is present at the location that /usr/libexec/java_home returns
          3) else if /System/Library/Java/JavaVirtualMachines/CurrentJDK exists: /System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home/bin/java

          The Oracle Java JDK's do not install to /System/Library/Java/JavaVirtualMachines/CurrentJDK

          The patch that has been applied seems to attempt this, but in the wrong order (i.e. first do 3) and only when it fails to resolve a Java do 2))

          Show
          Hans Aikema added a comment - abu:classes aikebah$ whereis java /usr/bin/java And as mentioned earlier in this issue: on modern Mac OS the location of java should be examined using 1) if JAVA_HOME defined: $JAVA_HOME/bin/java 2) else if /usr/libexec/java_home is present at the location that /usr/libexec/java_home returns 3) else if /System/Library/Java/JavaVirtualMachines/CurrentJDK exists: /System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home/bin/java The Oracle Java JDK's do not install to /System/Library/Java/JavaVirtualMachines/CurrentJDK The patch that has been applied seems to attempt this, but in the wrong order (i.e. first do 3) and only when it fails to resolve a Java do 2))
          Hide
          Christopher Tubbs added a comment -

          The patch that has been applied does it in that order because that was the way to preserve the previous (almost certainly incorrect) behavior of looking at CurrentJDK first. I agree the ordering Hans Aikema lists makes the most sense, but I think it's a question of whether or not to preserve the previous behavior or respect /usr/libexec/java_home first.

          Show
          Christopher Tubbs added a comment - The patch that has been applied does it in that order because that was the way to preserve the previous (almost certainly incorrect) behavior of looking at CurrentJDK first. I agree the ordering Hans Aikema lists makes the most sense, but I think it's a question of whether or not to preserve the previous behavior or respect /usr/libexec/java_home first.

            People

            • Assignee:
              Jason van Zyl
              Reporter:
              Alin Dreghiciu
            • Votes:
              12 Vote for this issue
              Watchers:
              13 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: