12

I have 2 JAVA version on my Macbook. I want to change version from 14 to 11. I found 2 solutions on the internet but both are not working.

My java -version result is

openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+14)
OpenJDK 64-Bit Server VM (build 14.0.1+14, mixed mode, sharing)

1st Solution (How to set or change the default Java (JDK) version on OS X?), I run /usr/libexec/java_home -V then I got a result below,

Matching Java Virtual Machines (2):
14.0.1, x86_64: "OpenJDK 14.0.1"    /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
11.0.8, x86_64: "Java SE 11.0.8"    /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

Then I did

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

After checking from java -version, The result is still 14.0.1

2nd solution (How to set JAVA_HOME in Mac permanently?), I edit ~/.bash_profile file as below,

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

And I run command

source ~/.bash_profile
echo $JAVA_HOME

It shows the result,

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

But when I check the result with java -version, It's still 14.0.1 as well

3
  • 4
    That is just the JAVA_HOME variable. You need to add $JAVA_HOME/bin to your $PATH. Please check your $PATH variable Commented Oct 13, 2020 at 13:25
  • Try this one, I believe the others are for the current session and terminal: stackoverflow.com/a/44169445/6950859 Commented Oct 13, 2020 at 13:27
  • If you use Anaconda and the Terminal starts with some environment (e.g., [base]), you won't be able to change Java unless you deactivate the environment by: conda config --set auto_activate_base false and restarting the Terminal Commented Mar 23 at 1:37

5 Answers 5

11

Install sdkman which takes care of the rather tedious command line voodoo you have to employ to try to make this happen. The problem is, JAVA_HOME is just an environment variable, it changes nothing - only tools that explicitly look for it (generally, maven and ant for example) will be affected by messing with it. When you type java on a mac, it runs /usr/bin/java, which is not a file you can change even as root. This java will then invoke the real java, and does not look at JAVA_HOME to get the job done: It is a softlink to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java, and because it is in /System you can't change that either, not even as root.

That's why this is so hard, and why you want a nice tool (sdkman) to do it for you.

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

2 Comments

Anybody who comes here after hours of debugging, just do what he says, works like a charm.
I just wish this answer included more than "use someone's software because it's too hard to explain how to get around these problems." If sdkman can fix it then it should be something we fix without sdkman.
9

The removal of quotes around the JAVA_HOME and then setting the path variable variable worked for me (Mac OS Catalina):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

Comments

6

If you need to switch between versions and already have the JDKs installed, you can adjust your bash profile with aliases that reset JAVA_HOME and reinitialize your PATH each time:

export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)

alias java11='export JAVA_HOME=$JAVA_11_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java14='export JAVA_HOME=$JAVA_14_HOME; export PATH=$JAVA_HOME/bin:$PATH'

Then, after restarting your terminal session, you can switch back and forth:

$ java11
$ java -version

and

$ java14
$ java -version

Comments

1

Try export JAVA_HOME='/usr/libexec/java_home -v 11' (without the minor version), it outputs the right java -version for me on Mac.

Comments

0

I also faced the same problem in my apple macbook pro with m1 arm chip. The solution is simple.

1: First You have to check that what are the installed JDK in your computer .For That you have to use the following command.

/usr/libexec/java_home -V

Above will give yout the versions like 20.0.1 (arm64) "Oracle Corporation" - "OpenJDK 20.0.1" /Users/buddhasattaduttaroy/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home 16.0.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home 11.0.20 (arm64) "Oracle Corporation" - "Java SE 11.0.20" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home 11.0.19 (arm64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/buddhasattaduttaroy/Library/Java/JavaVirtualMachines/corretto-11.0.19/Contents/Home

You have to select the version from here and then apply like this

export JAVA_HOME=/usr/libexec/java_home -v 1.8 ** mind it you dont have to specify the complete version of the java but the normal one (means not 11.0.6. but 11.o or 1.8 as per ur requirement)**

2:You have to put these command in the .bashrc and .zshrc and do source over them.

I think this will solve your problem

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.