Java Code Samples

Multiple versions of Java on OS X Mountain Lion

Posted in Java, Miscellaneous by Cristian on September 16, 2012

@This article is deprecated and might be used only for MounatinLion 10.8.1

Before Mountain Lion, Java was bundled inside OS X. It seems that during the upgrade, the Java 6 version I had on my machine was removed.
Apparently the reason for uninstalling Java during the upgrade process was caused by a security issue that the Java runtime had.In this way you are forced to install the latest version which fixed this security problem.

So I went to /Applications/Utilities/ open a Terminal and executed the following command:
java -version ==> “No Java runtime present …”
A window prompted asking to install Java.Click “Install” and get the latest version.I installed it but right after I downloaded and installed the JDK SE 7 from Oracle.

After installation, open the Java Preferences (Launchapad/Others ) and you will see :

Now I knew I had two versions of Java but which one I am using it ?

$ java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

So what if i want to use JDK SE 7 from Oracle ?
Then I had just to drag Java SE 7 in the Java Preferences window to the first position in the list.

This time :

$ java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

I said to myself let’s find out more out how Java is installed on OS X so I dug for more.

There are some very useful commands : whereis and which and ls -l.

whereis java ==> /usr/bin/java
ls -l /usr/bin/java ==> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

When I saw this I was a little bit curious so I went to list the Versions directory:

cd /System/Library/Frameworks/JavaVM.framework/Versions
ls ==>
1.4 1.5 1.6 A CurrentJDK
1.4.2 1.5.0 1.6.0 Current

Now why do I have this old versions of Java on my machine ? So I asked on Ask Different
http://apple.stackexchange.com/questions/57986/multiple-java-versions-support-on-os-x-and-java-home-location

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.8.1
BuildVersion:	12B19
$ ls -l /System/Library/Frameworks/JavaVM.framework/Versions
total 64
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Sep 16 15:55 1.6.0 -> CurrentJDK
drwxr-xr-x  7 root  wheel  238 Sep 16 16:08 A
lrwxr-xr-x  1 root  wheel    1 Sep 16 15:55 Current -> A
lrwxr-xr-x  1 root  wheel   59 Sep 16 15:55 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

It seems all the old versions are links to the CurrentJDK version , which is the Apple version, except A and Current which is linked to A.I read something about this on this question.For me A acts like a temp variable. If in Java Preferences you set the in the first position Java 6 from Apple A will have Java 6 from Apple if you put on the first position Java SE 7 from Oracle A will point to this version.Current points to A.

/java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)
./java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)

So it means that in this Current directory will point to the first Java Version found in the Java Preferences.
A very interesting thing is the following information

lrwxr-xr-x  1 root  wheel   59 Sep 16 15:55 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

This means Java from Apple is actually installed here :”/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/”
What about Java SE 7 ? I could search the filesystem to see but I found an easier way:

If in Java Preferences on the first position is Java SE 7 ==>

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

If in Java Preferences on the first position is Java SE 6 (System) ==>

$ /usr/libexec/java_home
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

So Java on Mountain Lion (OSX) is more likely to be installed in one of this locations :

  • /System/Library/Java/JavaVirtualMachines
  • /Library/Java/JavaVirtualMachines
  • ~/Library/Java/JavaVirtualMachines

What about /System/Library/Frameworks/JavaVM.framework/Versions ?
It seems that is linked to the so called “Java bridge“.Here it seems is the native part of the Java on OSX installation.

About these ads

3 Responses

Subscribe to comments with RSS.

  1. hgomez said, on September 18, 2012 at 3:26 pm

    Interesting article, very similar to mine :)

    http://blog.hgomez.net/2012/07/20/understanding-java-from-command-line-on-osx/

    • Cristian said, on September 18, 2012 at 4:45 pm

      My attention was more on Java Preferences and current java version based on the order you have there.

  2. About Java said, on December 26, 2012 at 12:47 pm

    good article.. i did the same what you are telling in this.. :)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: