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.
Setting default java version or JAVA_HOME
Follow below steps to make a particular java version as default or to switch among multiple java version on your Mac system.
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.
5. Now, to verify if the default java is changed, again type java -version and press return. Below is the output
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 .
Change Java Version in MacOS
- Use the Traditional Way to Change Java Version in MacOS
- Use jenv to Change Java Version in MacOS
In this article, we will discuss how to change the version of Java installed on a mac machine. When multiple Java installations of different versions are available on the local machine, we can switch between them using the following steps.
Use the Traditional Way to Change Java Version in MacOS
When installing the JDK package on our mac, we have to specify its path as an environment variable to access the Java tool anywhere in the local system. This environment variable is where we tell the version of Java to be used as default.
First, we need to change the current directory to /usr/libexec using the cd command.
Once we are in the /usr/libexec directory, we execute the ./java_home command with -V that returns the versions and locations of the installed Java.
Here is the output after running the command. We can see three versions of Java available; we focus on the first and the last Java version.
The first Java version is 14.0.1, while the second Java version is 1.8.
> 14.0.1 (x86_64) "Oracle Corporation" - "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home 1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
We use the command java with -version that returns the version of the default JDK.
> java version "14.0.1" 2020-04-14 Java(TM) SE Runtime Environment (build 14.0.1+7) Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
We can see the default JDK is Java version 14.0.1; if we want to change this version to 1.8, we need to follow the further steps.
switch java version on mac OS
Im trying to switch the java version with the following export JAVA_HOME=’/usr/libexec/java_home -v 1.8.0_172′ but when I run java -version I got the following java version «10.0.1» 2018-04-17 Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode) I want to switch to the 1.8.0_172 version in MAC how it can be done ?
@MarkRotteveel: No He doesn’t. OP uses quotes instead of backticks and there are other answers as well.
@lakshman I said trying, and most other answers there are variations on that theme. I think it would have been helpful to point out that mistake initially (as it is non-obvious to people not familiar with bash).
4 Answers 4
Assuming you have jdk1.8.0.172 installed, one option is:
I think the easiest for me was using jenv
It is similar to rvm or nvm to easily switch between java versions.
Steps:
echo ‘export PATH=»$HOME/.jenv/bin:$PATH»‘ >> ~/.bash_profile
echo ‘eval «$(jenv init -)»‘ >> ~/.bash_profile
echo ‘export PATH=»$HOME/.jenv/bin:$PATH»‘ >> ~/.zshrc
echo ‘eval «$(jenv init -)»‘ >> ~/.zshrc
In the terminal run unset JAVA_TOOL_OPTIONS (This is a precautionary measure in case you have output that breaks the text parsing. In my case this did make a difference)
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
jdk1.8.0_231.jdk -> Use whatever version you have on your machine.
Then use javac -version to verify it has been changed.
Change java version (Mac)
to my .zshrc file. But this seems to only change the path for my terminals java command. Also tried to change the HOME symlink like this:
cd /Library/Java mv Home Home-1.6 ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/ Home
This had no effect at all. Also tried java changer software: http://www.guigarage.com/2013/02/change-java-version-on-mac-os/ But no effect. Any idea how to start java programs like .app and .jar files with the 1.7 version by just clicking on them?
closed as off topic? «. unless they directly involve tools used primarily for programming.» The jdk is almost exclusively used for programming.
3 Answers 3
I believe OS X (at least 10.8) uses the following paths:
- JRE: /System/Library/Frameworks/JavaVM.framework/Versions/Current
- JDK: /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Those are symlinks, which you can update to point to your 1.7 installation.
You can verify this fairly easily:
a) run which java to check which java executable is being executed. In theory, that should be /usr/bin/java .
b) run ls -la on your java executable, which should tell you where it points ( /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java on my machine).
I think this should sort your .jar execution issue. If your Java application is wrapped in a .app , I believe it’s a bit more complex: if memory serves, the version of java used will depend on the JavaApplicationStub being used by the .app .