Setting java version mac

How do I set the default Java (JDK) version on Mac OS X?

In this post you will learn how to set the default JAVA_HOME in Mac OS X when you have more than one JDK installed in your computer. First you need to run /usr/libexec/java_home -V command to get the list of installed JDK. The command will print out something like the following depending on the available JDK in your computer.

On my machine I have the following version of Java.

Matching Java Virtual Machines (3): 9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home 1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home 1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home 

From the list above pick which version you want to be the default JDK. For example, I will choose the 1.8.0_121 version to be my default JDK. To set it run the command below.

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_121` 

If the major version of the available JDK is unique you can just use the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8` 

After setting the JAVA_HOME and you run the java -version command you will see that JDK 1.8 is the new default JDK in your computer.

java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) 

The change above will only be active in the current running shell. If you close or terminate the shell, next time you open the shell you will need to set it again. To make this change permanent you need to set it in your shell init file. For example if you are using bash then you can set the command in the .bash_profile . Add the following lines at the end of the file.

# Setting default JDK to version 1.8. export JAVA_HOME=`/usr/libexec/java_home -v 1.8` 

To activate this configuration right away your can run source .bash_profile . This command reads and executes the .bash_profile in the current shell.

Читайте также:  Php парсинг строки html

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

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 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.

# Show system default i.e. highest version of Java
/usr/libexec/java_home

# Output
/Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home

We can also check all the versions of Java installed on the system using the command as shown below.

# List installed JDKs
/usr/libexec/java_home -V

# List Output
Matching Java Virtual Machines (2):
11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home

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.

# List installed JDKs
/usr/libexec/java_home -V

# List Output
Matching Java Virtual Machines (2):
11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home

# Switch active JDK
export JAVA_HOME=`/usr/libexec/java_home -v `

# Switch Example 1
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_251`

# Check Active Java
java -version

# Version Check Output
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

# Switch Example 2
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.7`

# Check Active Java
java -version

# Version Check Output
java version "11.0.7" 2020-04-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)

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.

# Create .bash_profile if not exist
touch ~/.bash_profile

# Open .bash_profile for editing
open -a TextEdit.app ~/.bash_profile

# Specify default Java
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

# Save and close .bash_profile

This will preserve the default version of Java. Now close the terminal and open a new one.

# List Java Versions 
/usr/libexec/java_home -V

# List Output
Matching Java Virtual Machines (2):
11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home

# Test Active Java
java -version

# Active Java
java version "11.0.7" 2020-04-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)

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.

# Execute system preferences
source ~/.bash_profile

# Test Active Java
java -version

# Active Java
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

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.

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 .

Источник

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