Java Version Shuffle: How to Switch Java Versions on MacOS with Ease
Apple

Java Version Shuffle: How to Switch Java Versions on MacOS with Ease

2023-02-27 最後更新:2023-12-14) · 10 分鐘 · Ray Lee (System Analyst)

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.

English Version

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.

日本語版

MacでJavaバージョンを切り替える必要がある状況に陥ったことはありませんか?古いバージョンのJavaを必要とするアプリケーションがあったり、新しいバージョンの新機能を活用したい場合もあるでしょう。理由が何であれ、異なるJavaバージョン間の切り替え方法を理解するのは面倒なことがあります。この記事では、Mac上で異なるJavaバージョン間を簡単に切り替えるためのステップバイステップガイドを提供します。これで、すぐにコーディング(またはMinecraft!)に戻ることができます。さらに、軽い気持ちで読めるよう、途中でいくつかジョークも交えています!

macOSでは、java_homeコマンドを使用してデフォルトのJavaバージョンを切り替えることができます。手順は以下の通りです:

1. アプリケーション > ユーティリティフォルダからターミナルを開きます。

2. 以下のコマンドを入力して、インストールされているJavaバージョンを一覧表示します:

/usr/libexec/java_home -V

3. このコマンドは、インストールされているJavaバージョンの一覧を出力します。

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. バージョンを見つける

デフォルトとして設定したいバージョンを見つけ、そのインストールディレクトリのパスをコピーします。

以下のコマンドを実行してデフォルトのJavaバージョンを設定します。パスを使用したいバージョンのインストールディレクトリのパスに置き換えてください:

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

例えば、Java 11をデフォルトに設定したい場合は、以下のように使用します:

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

このデモでは、Java 1.8を使用しました。

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

このコマンドは、JAVA_HOME環境変数を指定したパスに設定し、システムのデフォルトJavaバージョンにします。

5. デフォルトのJavaバージョンが正しく設定されたか確認するには

以下のコマンドを実行します:java -version

java -version

このコマンドは、先ほどデフォルトとして設定したJavaのバージョンを出力するはずです。

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)

コンピュータの再起動後も変更は保持されるか

macOS Catalina(バージョン10.15)以降、デフォルトシェルがBash(~/.bash_profile)からZshに変更されました。そのため、Catalina以降のmacOSバージョンを使用している場合、.bash_profileがない可能性があります。

代わりに、ホームディレクトリに.zshrcファイルを作成または編集できます。nanoやvimなどのテキストエディタを使用してファイルを作成・編集できます。

.zshrcファイルを作成または編集する手順は以下の通りです:

Macでターミナルを開きます。

以下のコマンドを入力して.zshrcファイルを作成または編集します:

nano ~/.zshrc

これにより、nanoテキストエディタが開き、ホームディレクトリの.zshrcファイルが作成または開かれます。

JAVA_HOME環境変数を設定するために、以下の行をファイルに追加します:

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

パスを使用したいJDKバージョンのパスに置き換えてください。

Ctrl+Xを押してnanoを終了し、変更を保存します。

これによりファイルが再読み込みされ、変更が適用されます。

以下のコマンドを入力して.zshrcファイルを再読み込みします:

source ~/.zshrc

まとめ

まとめとして、macOSでのJavaバージョンの切り替えは、ターミナルの使用、環境変数の変更、またはJavaバージョンマネージャーの使用など、さまざまな方法で行うことができます。この記事で説明した手順に従うことで、ユーザーは異なるJavaバージョン間を簡単に切り替え、アプリケーションとの互換性の問題を回避できます。各アプリケーションに正しいJavaバージョンを使用することが、最適なパフォーマンスを確保し、潜在的なセキュリティ脆弱性を回避するために重要であることを覚えておいてください。

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)

このトピックをさらに深く掘り下げ、Java開発環境をより柔軟に管理するための高度なテクニックと方法を紹介します。

Ray Lee (System Analyst)
作者 Ray Lee (System Analyst)

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