Changing default Java in Mac OS X is bit tricky

Changing Default Java Version in Mac OS X (Yosemite)

Changing default Java in Mac OS X is bit tricky
Changing default Java in Mac OS X is bit tricky

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:

                    export JAVA_HOME=$(/usr/libexec/java_home -v version_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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s