Mac OS X 10.6.7 Java Path Current JDK confusing

JavaMacos

Java Problem Overview


I have trouble understanding (actual paths vs links) for multiple java versions on my Mac OSX. Normally in windows if I have multiple versions installed in my machine, I can just take the path of which ever version I want and use it. But in MAC OS X I undertand that there is something called links that is being pointed to CurrentJDK, and if I want to use a different version I will need to change the link to CurrentJdk right? But what is confusing for me is that as you can see below all my versions are pointing to the same CurrentJDK which means all versions point to current version? I was expecting only one of them would be pointing to CurrentJDK and I could just change it to which ever one I need which is not the case here. All I need to know is how to find the bin(Commands) folder path for each version, so that I can just use it to point to CurrentJDK? Also tell me how to change the link to CurrentJDK.

$ java -version

java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07-334-10M3326)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02-334, mixed mode)

$ pwd

/System/Library/Frameworks/JavaVM.framework/Versions


$ ls -l

lrwxr-xr-x   1 root  wheel    5 Mar 20 11:12 1.3 -> 1.3.1
drwxr-xr-x   3 root  wheel  102 Dec  2  2009 1.3.1
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.4 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Mar 20 11:12 1.6.0 -> CurrentJDK
drwxr-xr-x  10 root  wheel  340 Mar 20 11:13 A
lrwxr-xr-x   1 root  wheel    1 Mar 20 11:12 Current -> A
lrwxr-xr-x   1 root  wheel   59 Mar 20 11:12 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

Java Solutions


Solution 1 - Java

For different types of JDKs or installations, you will have different paths.

On Mac OS X Mavericks, the path I found as following:

  1. Built-in JRE default:

    /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

  2. JDKs downloaded from Apple:

    /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/

  3. JDKs downloaded from Oracle:

    /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

Solution 2 - Java

Most possibly you only have one jdk installed, thus all the soft links points to the same jdk. If you however did have multiple jdks installed you would need to choose which one to use with the 'Java Preferences' app in your /Application/Utilities.

Soft links are by the way created with the ln -s command from Terminal.

Solution 3 - Java

Just run following commands:

$ brew update
$ brew install jenv
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/
  oracle64-1.8.0.45 added
  1.8.0.45 added
  1.8 added

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
  oracle64-1.7.0.79 added
  1.7.0.79 added
  1.7 added

$ jenv versions
* system (set by /Users/wanluwang/.jenv/version)
  1.7
  1.7.0.79
  1.8
  1.8.0.45
  oracle64-1.7.0.79
  oracle64-1.8.0.45

$ java -version
  java version "1.8.0_45"
  Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
  Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

$ jenv global oracle64-1.7.0.79

$ java -version
  java version "1.7.0_79"
  Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
  Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Solution 4 - Java

From Apple Technical Q&A Java Questions:

User Preferences

On Mac OS X, the Java runtime provides the java.util.prefs API which is backed by the standard Mac OS X Preferences API and directories. Simply using this pure Java API reads and stores your application's preferences in ~/Library/Preferences in a Mac OS X property list file. For applications that may already have their own preferences format, these preferences should be stored in the ~/Library/Preferences directory as well. This directory can be reached from Java code by creating a file with the path of System.getProperty("user.home") + "/Library/Preferences/" + "com.example.your.Application". An application that should have global preferences across all users could instead reside in /Library/Preferences, however this directory is not writable by non-admin users.

Solution 5 - Java

set $JAVA_HOME=$(/usr/libexec/java_home) in your .bash_profile

or export JAVA_HOME=$(/usr/libexec/java_home);

echo $JAVA_HOME

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionreddyvaribabuView Question on Stackoverflow
Solution 1 - JavaChâu Hồng LĩnhView Answer on Stackoverflow
Solution 2 - JavaKarl-Bjørnar ØieView Answer on Stackoverflow
Solution 3 - JavaWanlu WangView Answer on Stackoverflow
Solution 4 - JavageorgeView Answer on Stackoverflow
Solution 5 - JavaAshish YadavView Answer on Stackoverflow