63
votes

Ok, I'm a bit new to Macs and OSX, but I picked one up so that I can do some troubleshooting on my Java programs with one since the company I work for uses a combination of OSX and Windows machines. The problem I'm running into is, when I install Java 7 from Oracle's website, it updates the preferences menu and appears to execute .jar files correctly when double-clicking them, but the terminal window's version is still 1.6.0_43 and running the same .jar file from the terminal results in runtime errors due to the older version.

When I navigate to /Library/Java/JavaVirtualMachines/ I'm presented with an empty folder. From what I've seen in other articles, this is where the Java 1.7.0's version folder should be. Any idea what's going on? How can I get the terminal to use the correct version of Java?

Edit: @DWilches comment on his original answer: (1)

total 64
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 17 21:38 1.6.0 -> CurrentJDK
drwxr-xr-x  8 root  wheel  272 Mar 17 21:38 A
lrwxr-xr-x  1 root  wheel    1 Mar 17 21:38 Current -> A
lrwxr-xr-x  1 root  wheel   59 Mar 17 21:38 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

(2)

ls -ld /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 Mar 17 21:38 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

Edit: sorry for the mistake with a new answer, gotten too used to sites that block edits of the original post after so long...

4
Java integration on Mac OSX is a ruinous mess atm, what with Apple discontinuing official support and Oracle not really knowing or caring how to integrate properly. If you can't find Java 7 in /Library/Java/JavaVirtualMachines then look in /System/Library/Frameworks/JavaVM.framework/Versions. It should be there, as a standalone directory or linked to Current.Perception
It is simple - Apple code and only Apple code is in /System, Third Party for all users are in /Librarymmmmmm
@DGolberg - what exact Java did you install Oracle provides the JRE and the JDKmmmmmm
@Mark I installed the JREDGolberg
As of Java 8 this should have improved much for Oracle Java.Thorbjørn Ravn Andersen

4 Answers

130
votes

JDK

On Mac OS, /usr/bin/java and friends are stubs that delegate to the real JDK commands. These stubs respect the setting of your JAVA_HOME environment variable, but for this to work you need to install the JDK (from http://www.oracle.com/technetwork/java/javase/downloads/index.html) as opposed to the JRE (from http://java.com).

The JDK installs into /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk (for whatever value of NN), so set your JAVA_HOME environment variable to /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home to make /usr/bin/java use 1.7. You can switch back to 1.6 simply by pointing your JAVA_HOME to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home instead. You can use the /usr/libexec/java_home tool to find the right value automatically, for example to make /usr/bin/java use Java 7 you can do

export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`

and to make it use Java 6 you can do

export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`

The same applies to Java 8 (using -v '1.8*'). This will pick up the latest installed JDK for the relevant major version, you don't need to remember to change the NN by hand when you install an update.

JRE

If you want to run the 1.7 or 1.8 JRE from the command line, it can be found in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java. This is a fixed path and you can only have one "public" JRE installed at any given time.

$ /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

You could use a shell alias in your .bashrc

alias java_jre='/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'
11
votes

The issue is that Oracle's JRE installation does not change the /usr/bin/java executable. If you want to use this Java you have to use the path under /Library (need to find what this is I have JDK install so could be different)

As per Oracle's JRE installation document

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.

The Oracle JDK does change /usr/bin/java to point to the Java 7 executable. If you are doing development then you should use this as it includes more than the JRE.

The Oracle install FAQ says

Q: Should I install the JRE or the JDK?

A: If you plan to run Java applications, install the Java Runtime Environment (JRE). The JRE is also referred to as Oracle Java. Once you have installed the JRE, you can launch Java applets and applications by double-clicking JAR files, JNLP files, and via the browser. Note that 32-bit browsers, such as Firefox in 32-bit mode, and Chrome, are not supported by the JRE.

If you plan to write Java applications, install the Java Development Kit (JDK).

5
votes

First, did you Quit the Console and open it again so it has chances of taking the changes in the PATH variable ?

If you've already done this, then check which the default version of your Java is:

dwilches@ ~$ cd /System/Library/Frameworks/JavaVM.framework/Versions/
dwilches@ Versions$ ls -l
lrwxr-xr-x  1 root  wheel   59 Mar 19 10:07 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.6.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel    1 Mar 19 10:08 Current -> A
drwxr-xr-x  8 root  wheel  272 Mar 25 10:03 A

Now, you can see that the "Current" version is "A" (that's my Java7). If I would like to change it to Java 1.6 then I could write:

dwilches@ Versions$ sudo unlink Current
dwilches@ Versions$ sudo ln -s 1.6 Current

And then:

dwilches@ Versions$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01-447-11M4203)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01-447, mixed mode)

So you may use this to make "Current" point to your desired Java location.

-1
votes

Just like Ian said, but probably you want java7 to be called from .sh script, so you need a function instead of alias added to .bash_profile:

java7() {
    /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java "$@"
}
export -f java7