Over a million developers have joined DZone.

Multiple Versions of Java on OS X Mavericks

Manipulate Java 7 and 8 from Oracle on OS X Mavericks.

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Of course after OS X Mountain Lion it was the time to update my java on OS X Mavericks.

Let’s see how to use multiple versions of java on OS X Mavericks.

I plan to use  Java7 and Java 8 from Oracle and to have an easy way of changing the default version.

As you probably know there is a difference between JRE and JDK.If not a very nice response you can find here.

Regarding JRE we should keep in mind that

1) “Only one JRE can be installed at a time. The system will not install a JRE that has a lower version than the current version. If you wish to install a lower version of the JRE, first uninstall the current version.”

2)”Installing a JRE from Oracle will not update java -version symlinks or add java to your path. To be able to do this, you need to install the JDK.”

I installed JdK 7 and Jdk 8

Right after I installed Jdk8 it seems that the simlinks were updated and java -version returned

MacCris:~ cristianchiovari$ java -version
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b05)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b05, mixed mode)
MacCris:~ cristianchiovari$

as well java_home was set to Jdk 8

MacCris:~ cristianchiovari$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

In my .profile i have as well defined

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

As well i found very practical this command which is used to choose the default java_home. (it seems it iterates all java installations and the latest version is tool)

MacCris:~ cristianchiovari$ /usr/libexec/java_home -verbose
Matching Java Virtual Machines (7):
1.8.0_20, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
1.7.0_11, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home
1.7.0_10, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home
1.7.0_05, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Now in order to have latest Jdk7 installed on my computer as default java I had to change in my .profile file

#export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"

After opening a new terminal

MacCris:~ cristianchiovari$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

Still

MacCris:~ cristianchiovari$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

That however is not affecting the default java version as described above.
In order to change that you might need to uninstall the Jdk that points to or to use some kind of hacked
as in http://stackoverflow.com/questions/17885494/how-can-i-change-mac-oss-default-java-vm-returned-from-usr-libexec-java-home

Inspired from :

http://docs.oracle.com/javase/7/docs/webnotes/install/index.html

https://developer.apple.com/library/mac/qa/qa1170/_index.html

http://stackoverflow.com/questions/15826202/where-is-java-7-installed-on-mac-os-x

http://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,devops ,osx

Published at DZone with permission of Cristian Chiovari, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}