6

I am using Amazon corretto JDK and earlier had only 1.8 installed in my Mac OS, yesterday I installed Amazon corretto JDK 11 using the Mac OS package installer and after that it changed my default java version to 11.

I followed highly rated medium blog on Jenv to manage multiple version on Mac OS but still my default java version is not switching back to 1.8 and didn't get any error while following stackoverflow answer or medium post.

command using Jenv

jenv versions

  system
  1.8
* 1.8.0.252 (set by /Users/my user/.jenv/version) // note `*` that should tell the current version IMO
  11
  11.0
  11.0.7
  corretto64-1.8.0.252
  corretto64-11.0.7

output of java -version

java --version openjdk 11.0.7 2020-04-14 LTS OpenJDK Runtime Environment Corretto-11.0.7.10.1 (build 11.0.7+10-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.7.10.1 (build 11.0.7+10-LTS, mixed mode)

Note: I have multiple apps which used different JDK versions and I don't want to use the alias way of managing the JDK version, I am interested in Jenv where I can set my JDK version globally(1.8 in my case) and locally(based on the JDK particular app uses).

  • In your case as you have two 1.8 versions (1.8 and 1.8.0.252). It is more than likely that you should update their Contents/Info.plist file section JVMVersion. Read my more complete answer bellow for more details. Regards. – Nicolas Dupouy May 26 '20 at 09:43
  • @NicolasDupouy thanks but I don't have two 1.8 version, if I run ` /usr/libexec/java_home -V` I get this ```Matching Java Virtual Machines (2): 11.0.7, x86_64: "Amazon Corretto 11" /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home 1.8.0_252, x86_64: "Amazon Corretto 8" /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home``` –  May 26 '20 at 09:46
  • Ok, then you should try `export JAVA_HOME='/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home'`. – Nicolas Dupouy May 26 '20 at 09:51
  • @NicolasDupouy, ok but can you tell me why jenv doesn't work? –  May 26 '20 at 09:59
  • I'am not sure, this is why I asked you to try this. – Nicolas Dupouy May 26 '20 at 10:08
  • As your `jenv version` shows you JDK If think you have configure it => `jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home` – Nicolas Dupouy May 26 '20 at 10:08
  • But I guess you should finish your configuration with: `jenv global 1.8.0.252`. Tell me if it fixes your issue. – Nicolas Dupouy May 26 '20 at 10:09
  • And I would guess that **1.8.0.252** is what is defined in **/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/Info.plist** file section ***JVMVersion***. – Nicolas Dupouy May 26 '20 at 10:11
  • 1
    @NicolasDupouy yes you are right `1.8.0.252 is what is defined in /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/Info.plist file section JVMVersion` abt this –  May 26 '20 at 11:13
  • Ok good. And did the `jenv global 1.8.0.252` fixed your jenv issue ? – Nicolas Dupouy May 26 '20 at 11:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214665/discussion-between-es-enthu-and-nicolas-dupouy). –  May 26 '20 at 11:38
  • @NicolasDupouy i've updated my question and made it explicit to `jenv` let me know if you need more info and thanks for guiding me –  May 27 '20 at 05:25

3 Answers3

26

The problem that you are seeing is because jenv is not setup properly for your terminal. Depending on your terminal, you should look at your .bash_profile or .zshrc and ensure that the following lines exist:

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

You can test to see if jenv has been initialized properly in your terminal by running which java. If jenv is initialized properly you will see something like:

/Users/your-username/.jenv/shims/java

If you don't see this, double check .bash_profile or .zshrc as described above. I also recommend that you place these at the end of the file so that you can be sure that jenv gets added onto the start of your PATH.

The magic behind jenv is jenv init. When you open your terminal this runs and updates your PATH to look at ~/.jenv/shims which contains a shim (shell script) which resolves your desired version of Java when you run java or other JRE/JDK commands.

Blaine
  • 652
  • 8
  • 11
3

Try adding this to your .bash_profile or .zshrc or .bashrc whatever is relevant to your macOS.

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }

Then, restart terminal and you can switch java versions like:

  • jdk 1.8
  • jdk 11
Saad Parwaiz
  • 131
  • 1
  • 4
2

Behind the scenes jenv uses the /Library/Java/JavaVirtualMachines directory.

Then you could also type /usr/libexec/java_home -V to show all the available JDKs and analyse your issue:

Matching Java Virtual Machines (4): 
11.0.2_2-OracleJDK, x86_64: "Java SE 11.0.2" /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home 
11.0.2_1-OpenJDK, x86_64: "OpenJDK 11.0.2" /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Home 
1.8.0_11, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home 
1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home 

/Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home

In this exemple export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) set the JDK 1.8 version.

Thus, you even could define the following aliases:

java11_OpenJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_1-OpenJDK)'
java11_OracleJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_2-OracleJDK)'
java7_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)'
java8_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)'

NB: The version to use after java_home -v is defined in each JDK installation in the Contents/Info.plist file section JVMVersion.

I changed it, to easily switch between the OpenJDK and the Oracle JDK:

$ tail -5 /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2
<key>JVMVersion</key>
<string>11.0.2_1-OpenJDK</string>

$ tail -5 /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2
<key>JVMVersion</key>
<string>11.0.2_2-OracleJDK</string>

So, in your case as you have two 1.8 versions (1.8 and 1.8.0.252). It is more than likely that you should update their Contents/Info.plist file section JVMVersion.

Nicolas Dupouy
  • 450
  • 1
  • 7
  • 11