Switching between Java 7 and 8 in OS X

25

18

I have installed Java-8. Now I wanted to go back to Java-7 by default so I type in...

/usr/libexec/java_home -v 1.7.0_40 --exec java -version
/usr/libexec/java_home -v 1.7.0_40 --exec javac -version

But I still see...

java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b108)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b50, mixed mode)

This does work...

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

But I would like a more permanent solution.

Anyone see what I am doing wrong?

Jackie

Posted 2013-09-25T20:13:21.413

Reputation: 677

Answers

50

Easily Switch Versions

  1. Install versions 1.6, 1.7, 1.8 in any order. Note: I believe the last one installed determines which one will be used for browser plugins, I'm not going to care about changing those below.

  2. Then, add to your ~/.bashrc or ~/.bash_profile, or where ever:

    #!/bin/bash
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
    
    setjdk() {
       export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    }
    
  3. Verify the change via java -version

    $ java -version
    java version "1.7.0_51"
    Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
    
    $ setjdk 1.6
    $ java -version
    java version "1.6.0_65"
    Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
    Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
    
    $ setjdk 1.8
    $ java -version
    java version "1.8.0"
    Java(TM) SE Runtime Environment (build 1.8.0-b132)
    Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
    

Obviously the change is only for the duration of the shell. But you can see where you can set it globally now.

nkadwa

Posted 2013-09-25T20:13:21.413

Reputation: 616

2

One problem: JDK7 refuses to install if you already have JDK8. It points you to a URL on oracle.com for more info that gives you a 404. And, of course, the uninstall instructions for JDK8 don't work at all, so now I'm basically lost.

– Eric W – 2014-06-22T14:35:49.633

1

just use jenv http://www.jenv.be

– madzohan – 2015-03-11T07:24:04.823

9

You may use jEnv (http://www.jenv.be/), which is "a command line tool to help you forget how to set the JAVA_HOME environment variable to switch between different versions of the JDK" (taken from the project's homepage).

If you're familiar with Ruby, JEnv is like using RVM or rbenv. It helps you handle several different JDKs installed on your machine without having to write your own script to switch from one JDK to another. You may change the current java version based for example on the current directory or based on a configuration file.

Pietro Di Bello

Posted 2013-09-25T20:13:21.413

Reputation: 191

Can you expand on this? This looks like a comment, not an answer. – Austin T French – 2014-05-02T10:44:28.090

If you're familiar with Ruby is like using RVM or rbenv to change the current java version based for example on the current directory or based on a configuration file. If you go to JENV home page there's a good tutorial. – Pietro Di Bello – 2014-05-13T08:01:41.297

This post should be for everyone who is looking for answers, so more detail in the post is expected here to be considered a complete and good answer. – Austin T French – 2014-05-13T10:11:39.307

I added more info and expanded a bit the answer... hope this looks better now. Thanks for the feedback. – Pietro Di Bello – 2014-05-13T20:52:31.433

4

You can't switch between Java-versions. In the Documentation you see:

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.

If you want to go back to an older version, you have to uninstall Java 8.

Christian

Posted 2013-09-25T20:13:21.413

Reputation: 6 571

grrr thanks but this doesn't help my opinion of OSX. – Jackie – 2013-09-26T01:28:48.997

4@Jackie: Actually this has little to do with OS X but more with Oracle. Back when Apple maintained the JDK for OS X it was perfectly, and easy, to switch between active Java versions. Please blame Oracle for their crappy installer and not OS X. – Alessandro Vermeulen – 2013-11-12T20:14:35.263

@AlessandroVermeulen with all due respect it isn't the "installer" heck I wish it didn't come with a version preinstalled period like my linux boxes. Then I could "install" it anyway I wanted with the unix binary. And then there were the older versions of OSX where it was even required to boot. IMHO it is absolutely the conventions used in OSX that cause the problem. – Jackie – 2013-11-12T23:18:29.007

4There are multiple JDK's installed correctly on my machine it is Oracles's preference plugin that does not allow switch from 1.8 to 1.7, from the command line with a full path they all work successfully. /usr/libexec/java_home -V lists them all - and the Java 7 preference pane allowed the choice of the Java 7 JREs – user151019 – 2014-02-17T21:40:54.803

0

I want to supplement @nkadwa's answer. His suggestion worked, but only after also changing the symlink for CurrentSDK. So here is the modified code:

setjdk() {
    export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    sudo ln -nsf ${JAVA_HOME%/*} /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
}
setjdk 1.8

imdahmd

Posted 2013-09-25T20:13:21.413

Reputation: 101