javamacosjava-17correttojenv

Not able to switch the Java version to Java 17


Posting again since someone moved my question a community where there is not much activity and the solution provided there in the comment did not work.

I have installed Amazon Corretto Java 17 from here.

However, I am not able to switch the Java version to Java 17.

Following are the steps I have already tried:

1.

export JAVA_HOME=$(`/usr/libexec/java_home -v17`)
  1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home
  2. I have also check the highest version of Java using below command: /usr/libexec/java_home and it shows correct Java 17 version: /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home
  3. I have also checked all the versions of Java installed on my machine using command /usr/libexec/java_home -V and it correctly shows Java 8, 11, and 17:
17.0.1 (x86_64) "Amazon.com Inc." - "Amazon Corretto 17" /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home

11.0.9.1 (x86_64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/harsh.pamnani/Library/Java/JavaVirtualMachines/corretto-11.0.9.1/Contents/Home

1.8.0_275 (x86_64) "Amazon" - "Amazon Corretto 8" /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home
  1. I also added JAVA_HOME exports mentioned in step-1 and step-2 to .zshrc and .bash_profile as well.
  2. I have followed multiple questions on StackOverflow and multiple blogs as well. For example,

How to set or change the default Java (JDK) version on macOS?

switch java version on mac OS

https://java.tutorials24x7.com/blog/how-to-switch-java-version-on-mac

https://www.lotharschulz.info/2019/08/21/mac-change-default-java-version

https://akrabat.com/using-jenv-to-select-java-version-on-macos/

https://www.happycoders.eu/java/how-to-switch-multiple-java-versions-windows/

  1. I am using jenv to switch between different java versions. Even jenv is not able to find Java 17. I used jenv versions and following is the output:
system
  1.8
  1.8.0.275
  11
  11.0
* 11.0.9.1 (set by /Users/harsh.pamnani/.jenv/version)
  corretto64-1.8.0.275
  corretto64-11.0.9.1
  1. I have also checked that JAVA_HOME is pointing to Java 17. Here is the screenshot: Screenshot


Even after following all the steps above when I do java --version, it is still set to Java 11:

openjdk 11.0.9.1 2020-11-04 LTS
OpenJDK Runtime Environment Corretto-11.0.9.12.1 (build 11.0.9.1+12-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.9.12.1 (build 11.0.9.1+12-LTS, mixed mode)
  1. I have also checked PATH variable, and nothing seems to be relatable to Java. Following is the output for path variables:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/harsh.pamnani/Downloads/apache-maven-3.6.3/bin

Could someone please help me understand if I am missing anything here. Thank you.


Solution

  • which java is often used to find the exact executable you are calling when you type in java.

    Keep in mind that when you type in the command java your JAVA_HOME setting is not used. Rather, the operating system PATH setting is used. If you have an older copy of java "before" the one you want to use on the path, then that's what the operating system will give you. To fix a problem like this, you need to alter your path such that the directory of your desired java version comes before any other java versions.

    I do see that you checked your PATH setting, but I think you didn't know what to check, because you only listed directories. In those directories, a java executable exists. If the old executable comes in a directory earlier in the path, that's the version of java you will get when running it from the command line, regardless of any other settings.

    Now, it is still important to have the JAVA_HOME set correctly, because when various Java tools want to discover items, thy might read JAVA_HOME (and if it is wrong, get directed to a non-matching JVM).