Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 524 posts at DZone. You can read more from them at their website. View Full User Profile

Java: Finding/Setting JDK/$JAVA_HOME on Mac OS X

06.17.2013
| 22276 views |
  • submit to reddit

As long as I’ve been using a Mac I always understood that if you needed to set $JAVA_HOME for any program, it should be set to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK.

On my machine this points to the 1.6 JDK:

$ ls -alh /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

This was a bit surprising to me since I’ve actually got Java 7 installed on the machine as well so I’d assumed the symlink would have been changed:

$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

Andres and I were looking at something around this yesterday and wanted to set $JAVA_HOME to the location of the 1.7 JDK on the system if it had been installed.

We eventually came across the following article which explains that you can use the /usr/libexec/java_homecommand line tool to do this.

For example, if we want to find where the 1.7 JDK is we could run the following:

$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home

And if we want 1.6 the following does the trick:

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

We can also list all the JVMs installed on the machine:

$ /usr/libexec/java_home  -V
Matching Java Virtual Machines (3):
    1.7.0_09, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home
    1.6.0_45-b06-451, x86_64:	"Java SE 6"	/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_45-b06-451, i386:	"Java SE 6"	/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
 
/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home

I’m not sure how I’ve never come across this command before but it seems pretty neat.

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Stephen Coy replied on Mon, 2013/06/17 - 9:47pm

I have the following aliases defined in my ~/.bash-profile:

alias java6="export JAVA_HOME=`/usr/libexec/java_home -v1.6`"
alias java7="export JAVA_HOME=`/usr/libexec/java_home -v1.7`"

This makes it very easy to flip JAVA_HOME values on the command line

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.