Can Java 7 and Java 8 co-exist on OSX

21,495

Solution 1

From a terminal: export JAVA_HOME=`/usr/libexec/java_home -v 1.x`, where x is the Java version.

I personally have a shell function that does that for me:

use-java () {
    export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`
}

I just have to call use-java 7 or use-java 8 in order to change my current shell's Java version.

Solution 2

Use jEnv.

If your system runs homebrew, you can install it using

brew install jenv

(You may need to run brew update to get the latest recipes first)

Add it to your bash profile using

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Start a new shell to make this change to the profile effective.

You can then add jdk’s like this:

jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

list the available versions using

jenv versions

And switch between environments using

jenv global oracle64-1.8.0.25

There’s plenty more custom options, like switching per directory or temporarily in a single shell, see http://www.jenv.be for those.

jEnv works by creating shim scripts for the java executables and putting them in the front of the path. Some 3rd party java tools like ant and maven rely on JAVA_HOME. To make sure JAVA_HOME gets set properly, run

jenv enable-plugin export

There's also jenv plugins for tools like maven and groovy.

Solution 3

Here is an excellent answer for how to switch Java version from the command line in OSX Mavericks (source by Neeme Praks):

Edit your ~/.bash_profile and add the following:

function setjdk() {  
  if [ $# -ne 0 ]; then  
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'  
   if [ -n "${JAVA_HOME+x}" ]; then  
    removeFromPath $JAVA_HOME  
   fi  
   export JAVA_HOME=`/usr/libexec/java_home -v $@`  
   export PATH=$JAVA_HOME/bin:$PATH  
  fi  
  echo JAVA_HOME set to $JAVA_HOME  
  java -version  
 }  
 function removeFromPath() {  
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")  
 }

(add above function to your .bash_profile)

Usage:

$ setjdk 1.7

Solution 4

After installation, open the Java Preferences (Launchapad/Others):

enter image description here

and drag the preferred version on top of list:

enter image description here

Share:
21,495

Related videos on Youtube

Dan
Author by

Dan

Updated on July 22, 2022

Comments

  • Dan
    Dan almost 2 years

    I've installed Java 8 for development purposes but now I'd like to use Java 7 again.

    How do I do this?

    It seems to be insanely difficult.

    Many thanks in advance. (I've tried using guigarage and that doesn't work)

    • Branislav Lazic
      Branislav Lazic over 10 years
      It depends. Generally, you should just change PATH variable value. But when it comes to development, it depends on IDE. You should see how to switch between JDK's for your IDE.
    • Dan
      Dan over 10 years
      IntellIJ won't start up at all because my default Java is Java 8.
    • Branislav Lazic
      Branislav Lazic over 10 years
      Try to remove your current JRE and install JRE 7 again.
    • Dan
      Dan over 10 years
      That's what I was trying to avoid - anyway how do I remove my current JRE? (I'm new to Mac is there an uninstaller?) Thanks
    • Branislav Lazic
      Branislav Lazic over 10 years
      Google is your friend. I'm not a Mac user. Sorry.
    • Dan
      Dan over 10 years
      I can assure you I've tried my friend Google... looking for new friends.
    • Anya Shenanigans
      Anya Shenanigans over 10 years
      All the VMs live here: /Library/Java/JavaVirtualMachines. mac os x will pick the biggest one by default - so java will invoke java 1.8 by default if it's installed there. You can move it somewhere else and it should work from that location as long as you set the path manually
    • Anya Shenanigans
      Anya Shenanigans over 10 years
      to fix the control panel issue, you need to reinstall java 7 - the internet plugin has been replaced by the one from java 8, which may cause problems.
  • Dan
    Dan over 10 years
    On my version of Mac (10.9.1) the screen looks slightly different. You open it from System Preferences. It only shows my current version of Java (java 8) in the list.
  • user987339
    user987339 over 10 years
    Install java 7 again, and it will be added to the list.
  • Dan
    Dan over 10 years
    I did that now it says that the platform is 1.8 but the product is 1.7?? java -version says 1.8. Do you know how I delete java 8 completely from my machine?
  • user987339
    user987339 over 10 years
    Can you post a screenshot of Java preferences dialog?
  • Dan
    Dan over 10 years
    Thanks - but as I understand it that's only useful if your program uses the JAVA_HOME variable in it's start script. What if you are launching eclipse or Intellij or something that just uses the default java on the machine. Can you change that?
  • Samy Dindane
    Samy Dindane over 10 years
    JAVA_HOME specifies the path of the JDK you'd be using in your terminal (for java or javac for example). It doesn't impact Java version outside of the terminal. IDEs have their own JDK versions configuration. In IntelliJ, you'd need to open the project preferences, go to the Project panel, and choose the JDK version you want to use.
  • Dan
    Dan over 10 years
    I'm talking about actually running IntelliJ (the IDE is a Java program).
  • Samy Dindane
    Samy Dindane over 10 years
    Oh sorry. That what I've understood from your question, but then I hesitated and edited it. You need to edit <key>JVMVersion</key><string>1.7*</string> in /Applications/IntelliJ IDEA XXX.app/Contents/Info.plist.
  • Shakti Garg
    Shakti Garg about 7 years
    if use zsh, use below commands to add it to .zshrc $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc $ echo 'eval "$(jenv init -)"' >> ~/.zshrc