Have you ever found yourself in a situation where you needed to switch between Java versions on your Mac? Maybe you have an application that requires an older version of Java, or perhaps you want to take advantage of new features in a newer version. Whatever the reason, it can be frustrating to figure out how to switch between different Java versions. In this article, we’ll provide a step-by-step guide to help you easily switch between different Java versions on your Mac, so you can get back to coding (or playing Minecraft!) in no time. Plus, we’ll throw in a few jokes along the way to keep things light-hearted!

On macOS, you can switch the default Java version by using the java_home command. Here are the steps:

1. Open Terminal from the Applications > Utilities folder.

2. Type the following command to list the installed Java versions:

/usr/libexec/java_home -V

3. This command will output a list of installed Java versions.

Matching Java Virtual Machines (5):
    11.0.14 (x86_64) "Oracle Corporation" - "Java SE 11.0.14" /Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home
    11.0.10 (x86_64) "Oracle Corporation" - "Java SE 11.0.10" /Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home
    1.8.351.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_351 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_351.jdk/Contents/Home
    1.7.0_80 (x86_64) "Oracle Corporation" - "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

4. Find the version

that you want to set as the default and copy the path to its installation directory.

Set the default Java version by running the following command, replacing the path with the path to the installation directory of the version you want to use:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/<jdk.version>/Contents/Home

For example, if you want to set Java 11 as the default, you would use:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.13.jdk/Contents/Home

For this demo, I used Java 1.8.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_351.jdk/Contents/Home

This command sets the JAVA_HOME environment variable to the path you specified, making it the default Java version for your system.

5. To verify that the default Java version has been set correctly

Run the following command: java -version

java -version

This command should output the version of Java that you just set as the default.

java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)

Do changes persist even after a computer restart

Starting from macOS Catalina (version 10.15), the default shell has been changed from Bash (~/.bash_profile) to Zsh. Therefore, if you are using a macOS version higher than Catalina, you may not have a .bash_profile.

Instead, you can create or edit a .zshrc file in your home directory. You can use a text editor like nano or vim to create and edit the file.

Here are the steps to create or edit the .zshrc file:

Open Terminal on your Mac.

Type the following command to create or edit the .zshrc file:

nano ~/.zshrc

This will open the nano text editor and create or open the .zshrc file in your home directory.

Add the following line to the file to set the JAVA_HOME environment variable:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.8.0_351/Contents/Home

Replace the path with the path to the JDK version you want to use.

Press Ctrl+X to exit nano and save the changes.

This will reload the file and apply the changes.

Type the following command to create or edit the .zshrc file:

source ~/.zshrc

Conclusion

In conclusion, switching Java versions on macOS can be done through various methods, including using the Terminal, modifying environment variables, or using a Java version manager. By following the steps outlined in this article, users can easily switch between different Java versions and avoid compatibility issues with their applications. It’s important to keep in mind that using the correct Java version for each application is crucial to ensure optimal performance and avoid any potential security vulnerabilities.

export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
export PATH="/opt/apache-maven-3.6.3/bin:$PATH"
#export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home"
export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.19)
#export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.14)
#export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
#export JAVA_HOME=$(/usr/libexec/java_home -v 1.7.0)

We’ll delve deeper into this topic, introducing advanced techniques and methods to manage your Java development environment more flexibly.

By Ray Lee (System Analyst)

iDempeire ERP Contributor, 經濟部中小企業處財務管理顧問 李寶瑞

Leave a Reply

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