How to set java –version corresponding to JAVA_HOME on Mac OS 12.6 – Java

by
Ali Hasan
java-17 java-8 macos

Quick Fix: Ensure JAVA_HOME environment variable points to the desired Java version, but avoid modifying PATH to include specific Java installations. Instead, rely solely on JAVA_HOME to locate the correct Java binaries.

The Problem:

On Mac OS 12.6, a user has multiple versions of JDK installed and wants to use Java 17. However, the java -version command is displaying the version of Java 8 instead of Java 17, even though the $JAVA_HOME environment variable is set to the correct Java 17 installation directory. The user needs a solution to update the java -version command to point to Java 17.

The Solutions:

Solution 1: Use JAVA_HOME to set Java version

On macOS, the commands for Java, such as java -version, are front-ends that rely on the JAVA_HOME variable to determine the correct Java version to use.

In this case, the java -version command is pointing to Java 8 because the PATH variable has been set to include the Java 8 installation. To resolve this issue, it is recommended to set JAVA_HOME to the desired Java version (Java 17 in this case) and avoid modifying the PATH.

Solution 2: Using Aliases or Executables

To automate the process of setting the JAVA_HOME environment variable and PATH, you can use aliases or an executable script. For instance, to set up Java 11 and a specific Maven version:

cp ~/.bash-profile-java-11 ~/.bash-profile

By doing this, you can easily switch between different Java versions and Maven installations without modifying your .bash_profile file manually.

Solution 3: Override PATH

To override the PATH variable and prioritize the preferred Java version, create an alias that sets JAVA_HOME to the desired JDK’s path and adds its bin directory to the beginning of PATH.

An example alias for Java 17:

alias j17='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home;export PATH=$JAVA_HOME/bin:$PATH'

Run the alias to set the environment variables, then verify with java -version that the correct version is now being used.

Solution 4: Use aliases to switch between Java versions

To make `java -version` point to the desired Java version, you can create aliases for each version in your command line. An alias is a shortcut that assigns a specific command to a new name. The `alias` command is used to create aliases.

For example, to create an alias named `j8` that points to Java 8, you can use the following command in Terminal. Replace `/Library/Java/JavaVirtualMachines/openjdk-8.jdk/Contents/Home` with the actual path to your Java 8 installation.

alias j8='export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-8.jdk/Contents/Home'

To create an alias for Java 17, use a similar command, replacing `j8` with `j17` and updating the path to the Java 17 installation.

alias j17='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home'

Once you have created the aliases, you can use them to switch between Java versions. To use Java 8, run the `j8` alias. To use Java 17, run the `j17` alias.

j8
java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-bre_2023_01_22_03_30-b00)


j17
java -version
openjdk version "17.0.2" 2022-07-19
OpenJDK Runtime Environment (build 17.0.2+8-Debian-1)

Q&A

How to set java –version corresponding to JAVA_HOME on Mac OS 12.6

Don’t play with PATH in this case, only with JAVA_HOME

How to set java –version corresponding to JAVA_HOME on Mac OS 12.6

My workaround is settting up my environment by editing .bash_profile file

How to set java –version corresponding to JAVA_HOME on Mac OS 12.6

Just to get rid of some of the confusion about aliases and to show how JAVA_HOME and PATH interconnect, the following alias approach is what I use

Video Explanation:

The following video, titled "How to Install Java JDK on Mac OS and set JAVA HOME variable ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... Java JDK on Mac OS and set JAVA_HOME variable Step 1 - Check if Java is already installed java -version Step 2 - Install Homebrew /bin/bash ...