Mac os set 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.

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

Читайте также:  Python find modules in dir

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 .

Источник

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.

Источник

How to switch between Java LTS versions 8, 11 and 17 on Mac

Change Java version on Mac 11 BigSur & persist it is great. However, Java 17 is not included. This post includes Java LTS version 17 and shows how to switch between Java/JDK LTS versions 8, 11 and 17.

On Mac you can install Java/JVM with brew‘s openjdk formulae . That includes JAVA LTS releases 17, 11, 8:

# version 17 brew install openjdk@17 sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk # version 11 brew install openjdk@11 sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk # version 8 brew install openjdk@8 sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

Setting the symlink after every installation step is important for the system Java wrappers to find the installed JDK. Installing JDKs with brew also recommends adding /usr/local/opt/openjdkXX/bin to PATH.

javahome function

I prefer the javahome shell function below rather than adding new values to PATH.

javahome() < unset JAVA_HOME export JAVA_HOME=$(/usr/libexec/java_home -v "$1"); java -version >alias j1.8='javahome 1.8' alias j11='javahome 11' alias j17='javahome 17'

Make sure to add the javahome function to your shell init file (.zshrc for me) so it is available in all terminal sessions.

javahome function in action

Now you can change the java version on the terminal using the respective alias:

$ j17 openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment Homebrew (build 17.0.1+0) OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+0, mixed mode, sharing) $ j11 openjdk version "11.0.12" 2021-07-20 OpenJDK Runtime Environment Homebrew (build 11.0.12+0) OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode) $ j1.8 openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-bre_2021_10_20_23_15-b00) OpenJDK 64-Bit Server VM (build 25.312-b00, mixed mode)

function javahome & aliases in action

That is an option. On #Mac I use SDK manager https://sdkman.io/usage#listversions

The command “sdk use java ” might be more verbose but doesn’t require maintaining a file. It doesn’t require more than a handful of command to add new JDK as they are made available. There is no need to edit a file like .zshrc and creating symlinks like it would be the case in 4 months when #Java 18 is released.

My reply [1,2]: This is great feedback – Thank you. The command sdk list java is great because it does list the installed java versions as well as the one in use. The sdk use java lets you choose another installed version easily. sdk install java 17 did not work for me, I had to specify a specific version e.g. sdk install java 17.0.1-open . SDKman supports that with tab completion though. The default sdkman installation command
$ source «$HOME/.sdkman/bin/sdkman-init.sh»
changes the bash config files. In my case these were two files both, .zshrc & .bash_profile. The added snippet is
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK.
export SDKMAN_DIR=»$HOME/.sdkman»
[[ -s «$HOME/.sdkman/bin/sdkman-init.sh» ]] && source «$HOME/.sdkman/bin/sdkman-init.sh» and must be removed in case you want to uninstall sdkman (https://sdkman.io/install paragraph Uninstallation). Adding this snippet can be avoided following the https://sdkman.io/install paragraph Installing without Modifying Shell Config guide. However, I assume that’s an edge case. I prefer (more) explicit changes to .zshrc or similar files. Writing the changes myself is explicit enough IMO. This way I remember in case I need to troubleshoot something related or I need to uninstall packages. Almost all macs I observed so far have brew installed because its the missing package manager for macOS. I use brew regularly (e.g. osx_bootstrap.sh) because it just works for me. I see the point of using SDKman when you need a cli tool to easily manage multiple software development kits and its versions.

My reply: Use the aliases to set the default java version. You can use the snippet below in your .zshrc file (or similar):
javahome() unset JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v «$1»);
java -version
>alias j1.8=’javahome 1.8′
alias j11=’javahome 11′
alias j17=’javahome 17’j17 #this makes version 17 the default version until you change it
This will set java version 17 when ever your .zshrc file (or similar) is executed. HTH

On my Mac (mojave 10.14.6) installing java 11 failed with a compilation error “src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c:407:71: error: too few arguments to function call, expected 5, have 4”. I should probably upgrade my Mac OS, I know, but having been pretty happy sitting on Mojave. There is also a message from brew saying: “OpenJDK is somewhat broken on newer MacOS instances, console is flooded with errors when using JMeter, AdoptOpenJDK has no issues https://github.com/Homebrew/homebrew-core/issues/66953” ?

My reply: The scripts run on Monterey 12. Some of my previous attempts are based on Big Sur 11 (e.g. https://www.lotharschulz.info/2021/01/11/change-default-java-version-on-macos-11-bigsur-persist-it/) .
However, I can’t really help with your Mojave apart from upgrading the OS 😉 .

Andy Bulka shared an update on medium: Turns out that adoptopenjdk offer brew installation of prebuilt binaries, which avoids the need for compilation. I was able to install java 17 on Mojave with brew install — cask temurin (adoptopenjdk has been renamed temurin).

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Оцените статью