25

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?

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.

  • 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 Jun 22 '14 at 14:35
  • 1
    just use jenv jenv.be – madzohan Mar 11 '15 at 7:24
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.

  • Can you expand on this? This looks like a comment, not an answer. – Austin T French May 2 '14 at 10:44
  • 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 May 13 '14 at 8:01
  • 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 May 13 '14 at 10:11
  • I added more info and expanded a bit the answer... hope this looks better now. Thanks for the feedback. – Pietro Di Bello May 13 '14 at 20:52
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.

  • grrr thanks but this doesn't help my opinion of OSX. – Jackie Sep 26 '13 at 1:28
  • 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 Nov 12 '13 at 20:14
  • @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 Nov 12 '13 at 23:18
  • 4
    There 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 Feb 17 '14 at 21:40
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.