Tuesday, December 02, 2014

Mac OSX Won't Acknowledge New JDK, Set Java Version

Java on OSX has always been much messier than Java on Linux.  The very way that Apple finds the current JDK is simply in-elegant and to be honest, quite ugly.  I've been shifting Java JDK's and JVM's back and forth for quite a while now in my dev machine and ran into this problem many times.  I tried using the System Preferences to set the default Java to 1.7.  It stated that it was 1.7 but every time I went back to the Terminal, I ran "java -version" and
got the old 1.6.  I tried everything!!  Nothing seemed to work as promised.

After some research, I found this little tidbit of help that worked.  It seems that there is a file installed with OSX that tells the OS what version of Java to use.  The file is a symbolic link to the location of the Java you want to use.

To find what version of Java you have, open the Terminal and type "java -version".  The output will be something like this:

java version 1.6
Java (TM) SF Runtime Environment (build 1.6.0_22-b11)
Java HotSpot (TM) 64-Bit Server VM (build 1.6.0_22-b11 - mixed mode)

OK.  So you have already installed Java 1.7 and tried to set the JAVA-HOME environmental variable, tried the System Preferences and nothing works?  Try this.

1. Install the new version of Java.

2. Open System Preferences and click on the Java icon. Open it up and make sure that Java 1.7 is selected.

3. Open up your Terminal and navigate to /System/Library/Frameworks/JavaVM.framework/Versions/ .  This is done by using the command "cd /System/Library/Frameworks/JavaVM.framework/Versions/".

There is a file there named "CurrentJDK".  This is simply a symbolic link to the Java home directory. Do not delete it, but do the following:

4. In Terminal, type "sudo mv ./CurrentJDK ./CurrentJDK_old". This simply renames the file in case something goes wrong.  To execute a command with sudo, you will be asked for your password. This is the admin password to your computer. Go ahead and enter it.

The CurrentJDK symlink was pointing to an older Java version. Removing this file (directories are actually "files" in Unix systems)  and create a new one pointing to the new Java JDK.  Since the current directory is owned by a privileged admin, you will need to use the sudo command.

5. In the Terminal, type "sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/ ./CurrentJDK" 

6. In the Terminal, type "ls -la" to make sure your new file was created and the old one is there, renamed.  You should see something like this:

That is it!  You may delete the old CurrentJDK_old file if you wish to.  If you now type in "java -version", you should see the new Java.

If this worked for you, please consider sharing this on Facebook or Twitter.  

No comments:

Post a Comment

Do not spam this blog! Google and Yahoo DO NOT follow comment links for SEO. If you post an unrelated link advertising a company or service, you will be reported immediately for spam and your link deleted within 30 minutes. If you want to sponsor a post, please let us know by reaching out to duane dot nickull at gmail dot com.