TIP: For Microsoft Windows users, please checkout my other article: https://medium.com/@bryantjiminson/changing-default-java-version-in-microsoft-windows-674f2d6d0955
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.
- Open a Terminal window
- Type java -version and Enter to check which version of Java you have as a default
- Download the appropriate JDK from Oracle website for Mac OS
- Once you installed the JDK, cd into /usr/libexec from your Terminal window
- Type the following command: ./java_home -V Make sure that you put the capital letter V and Enter
- You will see all installed versions of Java in your Mac OS X
- Now, open ~/.bash_profile with a text editor. For example, with VIM, you will type vim ~/.bash_profile
- Add the following command at the start:
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.