Saturday, July 2, 2016

Changing Your JDK on OS X

So you installed the newest and greatest JDK, maybe an early release. You have buyer's remorse, and want to go back to an older version of the JDK.

Good luck with that.

I'm joking!

First you need to know what versions of the JDK are currently on your OS X box. Check in the following directory.

/Library/Java/JavaVirtualMachines

That should have a list of your installed JDK's.

Alternatively, you can see your installed JDK's using the following command.

/usr/libexec/java_home -V

This list may include additional JDK's if you have a computer that's been around the block a few times.

Now, the real trick is running the one you want. If you just want to run a specific command using a specific JDK, then you might consider using the java_home tool.


Most developers of Java on OS X don't realize that they have a tool called java_home. It's not on the path by default. It is in the /usr/libexec directory. The java_home tool is very handy for issuing a command to run a Java app with the specified version of Java. For example, if you want to run javac with version 1.8.0_92 you could use the following command.

/usr/libexec/java_home -v 1.8.0_92 --exec javac -version

That would run the javac -version command with the 1.8.0_92 version of Java. The current default version of Java remains unchanged, so if you only need one app running with a different version of Java, the java_home command works great. Also note that running javac -version without the java_home command still runs javac under the default version of Java. So, you don't lose your default Java settings, just because you want to run one program with an older version of Java.

So if you want to change the default version of Java on OS X, where do you do that? That requires a bit of surgery with admin privileges. Also, you're going to need the installer for the JDK version you want as your default OS X JDK.

To change your default JDK, delete the following.

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin

and

/Library/PreferencePanes/JavaControlPanel.prefPane

I used these commands.

cd /Library/Internet Plug-Ins
sudo rm -rf JavaAppletPlugin.plugin
cd /Library/PreferencePanes
sudo rm JavaControlPanel.prefPane

Now run the JDK installer for the Java version you want as your default JDK.

You may find this only partially fixes your JDK version. The Java preferences panel may show your desired Java version, but running /usr/libexec/java_home still spits out the newer version of Java that you don't want as your default version.

If you're having this problem try exporting JAVA_HOME. You can do that permanently by adding the the following command to ~/.bash_profile substituting the version of Java you want for the 1.8 version I inserted in the command.

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

You may find that does great for the Terminal, but does nothing for helping Java based applications like Eclipse to run the correct version of Java. Isn't OS X great? *sigh*

In cases like that you might want to configure your applications to pick the right Java version. In Eclipse, for instance, you can browse into the app package and change the ini file to have a line like the following.

-vm
/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/bin

Now, suppose after you've gone through all this effort to make the older version of Java your default, you want to run a command or Java tool only run in a newer version of Java than the older one that you installed? Let's use jshell as an example. If you set your default OS X JDK to Java 8 and JShell is only in Java 9 and above, here's how to run it.

/usr/libexec/java_home -v 9 --exec jshell

There you have the easy ... yeah, right ... way to get a newer version of Java on your OS X box for development purposes, but use an older version for finicky programs like Eclipse that don't do well with change.

6 comments:

  1. Well, I get easy solution for all Java related problems at http://www.java-forums.org/ so I visit this site more often.

    ReplyDelete
    Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

      Delete
  2. In the Java programming dialect, all source code is first composed in plain content records finishing with the .java expansion. Those source records are then assembled into .class documents by the javac compiler.
    web design lessons

    ReplyDelete
  3. This information is impressive; I am inspired by your post writing style & how continuously you describe this topic. Java Training in Chennai

    ReplyDelete
  4. Great post. I am actually getting ready to across this information, its very helpful for this blog. Also great with all of the valuable information you have keep up the good work you are doing well

    White Label Website Builder

    ReplyDelete
  5. Java technology was created by James gosling, along with a small group of software engineers at Sun Microsystems in the early 90's. forum for programmers

    ReplyDelete