- Switching Java (JDK) Versions on MacOS
- How To Switch Java Version On Mac
- List Installed Java
- Switch Java (JDK)
- Preserve Java Version
- Disable Java
- Summary
- How to set default java version on Mac OS
- Switching Java versions on Mac OS
- Share this:
- Related
- 3 Replies to “Switching Java versions on Mac OS”
- Leave a Reply Cancel reply
- How to Set JAVA_HOME on MAC
- Set JAVA_HOME Environment Variable
- Switch Between Java Versions
Switching Java (JDK) Versions on MacOS
Mar 26, 2019 · 2 min read
This tutorial will guide you on how to switch JAVA versions on your MacOS device. This can be useful for working with apps which don’t work with updated JAVA versions. For example, applications such as Cassandra will only work with JAVA version 8.
First, you need to make certain you have multiple JAVA versions installed.
- Open a new Terminal window and input:
Your output should look like:
Note that there are two JDKs available. I f you don’t notice the Java version you need to switch to, download and install the appropriate one from here (JDK 8 is represented as 1.8) . Once you have installed the appropriate JDK, repeat this step.
2. Take note of the JDK version you want to switch to. For example, “11.0” and “1.8” are the JDK versions available in the example above.
3. Switch to the desired version. For example, if you wish to switch to JDK 8, input the following line:
For 11.0, switch “1.8” with “11.0”
4. Check your JDK version by inputting into Terminal:
If you have followed all the steps correctly, the JDK version should correlate with the one you specified in the last step.
5. (Optional) To make this the default JDK version, input the following in Terminal:
Then, add your Terminal input from step 3 to this file:
Источник
How To Switch Java Version On Mac
It provides the steps required to switch the current Java among the multiple versions installed on the Mac machine.
We might be required to install multiple versions of Java on the same Mac machine. Also, we can assign only one installation to the Java command. This tutorial provides the steps required to switch among the multiple versions of Java installed on the same Mac machine.
List Installed Java
We can check the highest version of Java installed on the system using the command as shown below.
We can also check all the versions of Java installed on the system using the command as shown below.
The java_home command with argument -V lists all the Java versions installed on the system and shows the most recent or highest version at the top of the list. It also shows the most recent version or system default at the bottom of the list.
Switch Java (JDK)
This section provides the options to switch among the multiple versions of JDK installed on the Mac system.
This switches the Java version for the active shell. If you close the terminal and again check the Java version by opening a new terminal, your changes won’t be reflected since the Mac system will pick the highest version by default. You can follow the next section to preserve the Java version and switch the Java to default.
Preserve Java Version
We can save the default version of Java to the
/.bash_profile file and execute it to switch to the default version of Java.
This will preserve the default version of Java. Now close the terminal and open a new one.
The above-mentioned commands list all the JDKs installed on the system and defaults the active Java to the highest version installed on the system. Now we can switch to our preferred JDK by executing the .bash_profile script.
In this way, we can always switch to our preferred JDK by simply executing the .bash_profile script.
Notes: There is no way to set the default JDK on Mac system. It will simply pick the highest version installed on the system. Though, we can switch the active Java on the terminal using the .bash_profile script as shown above. The third-party solution jenv might be helpful in certain situations.
Disable Java
We can disable a specific version of Java by renaming its Info.plist file to Info.plist.disabled. This will prevent Mac to consider the Java version with Info.plist file renamed to Info.plist.disabled while getting the highest version installed on the system. It will still consider the highest version as the default Java.
The Info.plist location is similar to the examples shown below.
JDK 11.0.7 -> /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Info.plist
JDK 1.8.0_251 -> /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Info.plist
If we rename Info.plist file of JDK 11.0.7 to Info.plist.disabled, Mac system will ignore it and use JDK 1.8.0_251 as the default JDK since it’s the next highest one installed on my system. It might differ on your system.
Summary
This tutorial provided the steps required to list the multiple versions of Java installed on the Mac system and switch the active Java. It also provided the steps required to persist the Java version.
Источник
How to set default java version on Mac OS
It generally happens that there are multiple java versions installed on a Mac system but we want a specific version out of those to be set as default.
This is required so that if an application requiring java is executed, it will pick up the desired version to avoid any compatibility issues.
In this article, we will look at the steps to be taken on Mac OS to set a particular java version to be set as default by setting JAVA_HOME environment variable.
1. Open terminal window on Mac OS.
2. Type following command and press enter(or return).
You should see below output
Matching Java Virtual Machines (2):
15.0.1, x86_64: “OpenJDK 15.0.1” /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home
11.0.2, x86_64: “OpenJDK 11.0.2” /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
This shows that the system has two java versions(first column) installed.
If you can not see the desired version here, then download it and install first.
Refer this guide to install openjdk from a tar file on Mac OS .
3. Now check the current default java version by using command java -version .
This will print
openjdk version “11.0.2” 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Current java version being used in 11.0.2. Suppose, we want to change it to 15.0.1.
4. Type following command and press return
export JAVA_HOME=`/usr/libexec/java_home -v 15.0.1`
This will set JAVA_HOME variable to version 15.0.1.
openjdk version “15.0.1” 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9-18)
OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)
Note that this will only change the default java for the current terminal session.
If you close the terminal and open a new one, you will still see the older java version as the default.
6. To make it permanent default, set JAVA_HOME in .bash_profile file.
In the terminal type,
This will open a file and you should see an entry for JAVA_HOME in this file as below
If you do not see any such entry, then add one.
Change the desired version of jdk as the per the output of command in Step 2 above.
Save the file. Default version of java is now updated.
You can check it by opening a new terminal window and verifying the output of java -version .
Источник
Switching Java versions on Mac OS
The Java JDK install on MacOS has some interesting platform specific utilities, like
which tells you which Java version you’re currently using and where it’s installed. I have Java 9 currently installed and it tells me:
If you have multiple version installed, adding -V will list all the versions and where they’re installed:
The next logical question from here would be ‘how do I switch versions’? If you set your $PATH in a .profile or similar approach, you can eval java_home to add the current version to your path, but a neat trick discussed in one of the answers here (thanks to this SO user for this tip) uses the -v option to allow you to switch versions. Adding a couple of aliases to your .profile like this:
… and you’ve got a quick shortcut to switching between different versions.
Share this:
Related
3 Replies to “Switching Java versions on Mac OS”
The aliases do not work for me:
JARMAC:
jar$ alias java11=”export JAVA_HOME=/usr/libexec/java_home -v 11; java -version”
JARMAC:
jar$ java11
bash: export: `-v’: not a valid identifier
bash: export: `11′: not a valid identifier
Unable to locate an executable at “/usr/libexec/java_home/bin/java” (-1)
It needs back ticks to work:
JARMAC:
jar$ alias java10=”export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version”
JARMAC:
jar$ java10
java version “10” 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Источник
How to Set JAVA_HOME on MAC
In this tutorial, I am going to share with you how to set the JAVA_HOME environment variable on a new MAC. The new way of setting JAVA_HOME environment variable allows us to dynamically identify and use the current Java version installed on a computer as well as let us switch between Java versions if there is more than one installed.
Starting from Mac OS 10.5 there is a very convenient command that we can use to identify the installed Java version. For example, open the terminal window on your Mac and type the following command to print out the top Java version installed on your Mac
If you have more than one version installed and you want to see all of the versions available, then run the following command in the terminal window.
To print the current Java version installed, simply use
Set JAVA_HOME Environment Variable
To set the JAVA_HOME environment variable for a current user you will need to update the
/.profile file. I will use the vi text editor on Mac but you can use the text editor of your choice.
- Open the terminal windows and type:
This will open the .profile file for editing.
Add the following line
The /usr/libexec/java_home will return the current version of Java installed on the Mac computer.
to apply changes to the .profile file right away.
In the terminal window type
This will print out the current Java version installed on your Mac computer.
Switch Between Java Versions
If you have more than one Java version installed on your computer and you need to switch to using a different version, you can do so by adding the -v flag.
Let’s have a look at a short example.
To learn what Java versions I have installed on my Mac computer I will type the following in the terminal window:
this will return
Which tells that there are two Java versions installed and the top version is “jdk-13.0.1.jdk“.
To set a specific Java version I will need to:
- In the terminal window type
To open the .profile file for editing.
Add Java version I want to set. Like so:
or if I wanted to set Java version 13, I would do it this way
Now if you check Java version by typing
It should print the current Java version set in JAVA_HOME.
Источник