; last updated - 4 minutes read

Once upon a time, you could use Java without installing it. Nowadays, most operating systems make a developer's life difficult by trying to help. That's not a big deal if you need only one version of Java. However, if you need to switch between different versions, it doesn't suffice to set the environment variables PATH and JAVA_HOME. Here's what you need to do.

MacOS Big Sur (aka MacOS 11+)

Older versions of OSX were pretty straightforward. Most of the time setting the PATH and JAVA_HOME worked. That's changed with MacOS Big Sur. In a way, it's a good idea: just set the version number, and the operation system does the rest for you. The catch is you have to unset (sic!) the JAVA_HOME variable.

So this shell script does the trick:

unset JAVA_HOME /usr/libexec/java_home -v 15.0.1 export JAVA_VERSION=15.0.1

It doesn't have to be the precise version number. Replacing 15.0.1 with 15 does the trick, too, unless you want to distinguish between, say, 15.0.1 and 15.2.0.

The bad news is you can't switch between identical versions provided by different vendors. That's not as arcane as it sounds: MacOS treats GraalVM as just another Java distribution. You can't easily switch between GraalVM 11.0.9 and AdoptOpenJDK 11.0.9.

The only solution I found to do so is using brute force. Navigate to the folder /Library/Java/JavaVirtualMachines, cd into the Java versionSelecting a Java version. Image published at piqsels.com under a CC0 license by an unknown artist. you're not interested in, and rename the Contents folder. That's a fairly reliable way to convince MacOS to ignore that Java installation. When you need it again, you must restore the Contents folder. Chances are you don't have to destroy the other Java version with the same version number: if MacOS finds a Java version matching the requested version number, it stops looking for other versions.

SDKMAN!

I discovered this option recently, several months after writing the original article. SDKMAN! is a command-line tool for MaxOS, Linux and seral other operating systems based on Unix. It manages Java and a wide range of popular tools in the Java universe. It allows you to choose between Corretto, AdoptOpenJDK, GraalVM Community Edition, Liberica, OpenJDK, SapMachine, and even Zulu. I didn't test it yet, because after installing it noticed it doesn't support the Enterprise version of GraalVM, but it looks promising. It looks similar to update-alternatives on Linux systems (see below), only it also covers tools like Gradle, Maven, and sbt, as well as languages like Groovy or Ceylon.

MacOS alternative: jEnv

You can also install a Java version manager. I've read about a tool called jEnv. I assume it does the trick, too, but I didn't test it. As far as I can see, using jEnv isn't much easier than the manual approach.

Ubuntu (or Linux in general?)

The Unix approach is - well, it's typical Unix style. Open a terminal window and enter this line:

sudo update-alternatives --config java

After that, things are pretty straight-forward. You can select from a list of Java versions.

Windows

On Windows, the key to success is to never install a JRE or a JDK. You don't have to. Downloading the zip file and unpackaging is enough. Set the environment variables JAVA_HOME=C:\your\folder\of\choice\graalvm-ce-java11-20.3.0 and PATH=%JAVA_HOME%\bin;%PATH%, and you're good to go. Just make sure the JAVA_HOME folder contains the folders bin and lib and the PATH contains the file java.exe. Also make sure to add the Java path first in the PATH variable.

If you've run a Java installation, this approach may or may not work for you. If it doesn't, there's a file called java.exe in the Windows folder. Delete it (but don't forget to make a backup of the file, just in case!). But if I'm not mistaken, that's only necessary on older versions of Windows, from the 2010 era. I remember switching to another Java version was a pain in the ass at the time, but the blogosphere doesn't complain much about the topic, so it seems the problem has been fixed. In any case, Sven Woltmann has collected all the hints you need to install anything between Java 1.2 and Java 15 on your Windows machine.


Comments