8

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 ?

4
  • 2
    Possible duplicate of How to set or change the default Java (JDK) version on OS X? Commented Oct 2, 2018 at 15:27
  • @lakshman It looks like the OP is already trying to apply the solutions of that question. Commented Oct 2, 2018 at 15:29
  • 1
    @MarkRotteveel: No He doesn't. OP uses quotes instead of backticks and there are other answers as well. Commented Oct 2, 2018 at 15:35
  • 2
    @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). Commented Oct 2, 2018 at 15:38

5 Answers 5

9

Assuming you have jdk1.8.0.172 installed, one option is:

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

Sign up to request clarification or add additional context in comments.

1 Comment

to see the java versions you have available: ls /Library/Java/JavaVirtualMachines/
7

I think the easiest for me was using jenv

It is similar to rvm or nvm to easily switch between java versions.

Steps:

brew install jenv

Bash

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Zsh

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 -versionto verify it has been changed.

1 Comment

Thank you. I found this solution to be much more robust for future-proofing.
6

The above worked for me (removing quotes) with Mac OS Catalina, however I also had to set the path variable like below:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{java version.jdk}/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

Where {java_version.jdk} is any of the Java version listed after running

/usr/libexec/java_home -V 

Comments

2
/usr/libexec/java_home -V

The above command will give you all java versions installed in your system.

export JAVA_HOME=`/usr/libexec/java_home -v <VERSION YOU WANT TO USE FROM ABOVE LIST`>

Comments

1

1.Install all the java version you want on your mac, reference: https://www.oracle.com/java/technologies/downloads/

~ % ls /Library/Java/JavaVirtualMachines/
jdk-1.8.jdk jdk-11.jdk  jdk-17.jdk  jdk-21.jdk

2.Check all the Java version you've installed

/usr/libexec/java_home -V

3.Set up .zshenv

  alias j8="export JAVA_HOME=`(/usr/libexec/java_home -v1.8)`; java -version"
  alias j11="export JAVA_HOME=`(/usr/libexec/java_home -v11)`; java -version"
  alias j17="export JAVA_HOME=`(/usr/libexec/java_home -v17)`; java -version"
  alias j21="export JAVA_HOME=`(/usr/libexec/java_home -v21)`; java -version"

4.Set default JDK version globally on .zshrc

export JAVA_HOME=`(/usr/libexec/java_home -v1.8.xxxx)`

5.Finally, you can switch JDK version by j**

~ % j8
java version "1.8.0_391"
Java(TM) SE Runtime Environment (build 1.8.0_391-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.391-b13, mixed mode)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.