-1

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.

4
  • 1
    Do you need to switch between versions of Java, back and forth? Or do you just want to use Java 17 only, and dump Java 11? Commented Dec 4, 2021 at 3:33
  • I need to switch between different java versions, because some of my projects are on Java 11 and some of the projects are on Java 17. Commented Dec 4, 2021 at 4:19
  • Java is backward compatible. In other words Java 17 can run code that was compiled with JDK 11. Hence I don't think you need to switch between 11 and 17. Commented Dec 4, 2021 at 6:53
  • I know that. But, the reverse is not possible. i.e. Java 11 can not run Java 17 code. And as I have asked in my question above, my current java version is set to Java 11. So, if I am running any Java 17 project, the compilation fails. Commented Dec 4, 2021 at 19:41

1 Answer 1

2

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

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

7 Comments

Thanks a lot Edwin for the reply. I have checked all the directories under PATH as suggested by you. Only /usr/bin seems to have the java executable. And there is only one executable for java. Attaching a screenshot for you. share.getcloudapp.com/6quvgRrR Could you please help me understand how to identify whether this executable is for java 11. And if it is for java 11, how can I point it to java 17. Again, thanks a lot for the response.
@harshpamnani if you run /usr/bin/java -version it will tell you what version it is. Believe that number, as that's the only version that file will ever report (even if it is moved). If you need a different version, you have to find a different java executable file, and put it earlier in your path than the ones you don't want.
Thanks Edwin. I found out what the issue is. When I did /usr/bin/java -version, it showed correct Java 17 version. However, when I did which java, it was showing /Users/harsh.pamnani/.jenv/shims/java. I found the java version for that and it is pointing to Java 11. Attaching a screenshot share.getcloudapp.com/NQu7PbnE. I will raise the concern with jenv that it is not able to correctly identify Java 17 (maybe they don't have support for that yet since it is new). Thanks a lot Edwin. You have been a great help.
@harshpamnani Until you get your answer from jenv (which I tend to just not use, as it is more trouble than it is worth, and it is not part of "standard" java installations), you can just put a new PATH entry before this .jenv/shims entry (or better yet, remove the .jenv/shims entry. Personally the idea of installing a program to set a two environmental variables (PATH and JAVA_HOME) just sounds beyond silly to me. Just edit your ~/.bashrc (or equivalent) if you need per-user overrides.
@harshpamnani Well, if something needs a specific Java, I typically configure the IDE to reference it (if in development) or the shell script wrapper to have an explicit entry (as well as export the entry to the environment). If your program gets really big and fancy, you can even put that value in a conf file. That way you don't have to reconfigure your environment to match what you are working on, as each executable now has it's own configuration.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.