Category

Multiple Java VMs on OSX

Java logo

Multiple Java VM on OSX

Introduction

Table of Contents

Let’s start by reviewing the baroque installation of Java on OSX.

Try these (highlighted) commands in a terminal.

So, when you install Java, /usr/bin/java is the vm command. It’s a symbolic link to the “current version”.
In my case Current is a symlink to a directory named A.

A lot of “legacy” links in there. Right now, “Current” is the one we care about.

Let’s see what version your current default vm happens to be:

If you simply run java -version (no path), you get the same output.

In my case, I installed the OpenJDK preview of Java 9.

But wait. In the directory listing for /System/Library/Frameworks/JavaVM.framework/Versions there was no Java 9. Where are the 1.7+ VMs?

You can use /usr/libexec/java_home to find the names of your installed VMs.

So, the “newer” i.e. current VMs are in /Library/Java/JavaVirtualMachines.
That last line shows your current “default” VM. Run java_home with no arguments to verify.

OK. So what?
Let’s see what java_home with the -v (lower case v this time) flag and a VM version gives us.
(use the vm name in /Library/Java/JavaVirtualMachines without the jdk prefix)

So, this gives us the full path to the installed VMs.

What about Java 9? Well, OpenJDK’s Java 9 uses a different naming convention, so you simply use 9 as the version.

This is a way to set your environment variables in your shell login config file (.e.g. ~/.bash_profile, NB not .bashrc).

Of course, if you want to change your VM “on the fly”, you’ll have to remove the old VM path from PATH. Unfortunately, even with Bash you have to engage in some sed nonsense. If you know an easier way (than sed), let me know.

So, cool. In the terminal, you get the VM you want. What about things that aren’t run from the terminal. Like an IDE? If you run Eclipse with the current snapshot of Java 9, it will crash. Setting your environment variables in .bash_profile does not affect these launches.

Eclipse

Table of Contents

If you have Java 9 installed, you won’t be able to run Eclipse. The solution is to edit Eclipse’s config file to use a specific VM. Here is mine. I added the lines -vm and the path to Java 1.8.

Global variables

Table of Contents

How do you set environment variables globally on OSX?

The current way (in El Capitan) is to create a plist in ~/Library/LaunchAgents/ that will be read by launchctl. In older versions of OSX, you edited /etc/launchd.conf

Eclipse seems to ignore these variables though. The eclipse.ini trick works.

Summary

Table of Contents

Java on OSX is a bit of a mess.

Resources

Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.