Changing Default Java Version in Mac OS X (Yosemite)

TIP: For Microsoft Windows users, please checkout my other article:

You may installed a Java Development Kit (JDK) already in your Mac OS X, but what do you do you if your currently installed JDK version is not compatible? Well, I personally had situations like that. More recently, I had to compile a Java project with JDK 1.7 because Google App Engine had a trouble running the project built in JDK 1.8. But my Mac OS X Yosemite already installed JDK 1.8 in my system. So, I had to downgrade the JDK to 1.7 to compile the project. Here is what I did.

  1. Open a Terminal window
  2. Type java -version and Enter to check which version of Java you have as a default
  3. Download the appropriate JDK from Oracle website for Mac OS
  4. Once you installed the JDK, cd into /usr/libexec from your Terminal window
  5. Type the following command: ./java_home -V Make sure that you put the capital letter V and Enter
  6. You will see all installed versions of Java in your Mac OS X
  7. Now, open ~/.bash_profile with a text editor. For example, with VIM, you will type vim ~/.bash_profile
  8. Add the following command at the start:

exportJAVA_HOME=$(/usr/libexec/java_home -vversion_of_your_java)

Replace the version_of_your_java with the JDK version you want to set. For example, JDK 1.7 will be 1.7.

Save the file (for VIM, it is x! in a Command mode) and start a new Terminal window. Type java -version again to see the default Java version changed!

Hope you found this useful and please leave me a feedback so I can hear what you think.


No Responses

Write a response