-1

I installed java jdk 20 and jdk 17 through SDKMAN.

I want to be able to do something like this.

alias java17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"

But when i do this

/usr/libexec/java_home -V

It gives me output like this

Matching Java Virtual Machines (3):     11.0.19 (x86_64) "OpenLogic-OpenJDK" - "OpenLogic-OpenJDK 11" /Library/Java/JavaVirtualMachines/openlogic-openjdk-11.jdk/Contents/Home     1.8.381.09 (arm64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home     1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home /Library/Java/JavaVirtualMachines/openlogic-openjdk-11.jdk/Contents/Home

Both Jdk20 and jdk17 are undetected by java_home

path of my jdk20 and jdk17 are -
/Users/default/.sdkman/candidates/java/20.0.2-oracle

/Users/default/.sdkman/candidates/java/17.0.8-oracle

I want java_home to detect jdk17 and jdk20 while I do this

/usr/libexec/java_home -V

I am unable to make java_home detect jdk17 and jdk20. I looked up on internet but didn't find any solution.

Though I found one work around method in which we imitate the folder structure of JDKs installed without SDKMAN.

This is how jdks installed without sdkman looks likee

  Contents/
    Info.plist
    Home/
      <actual JDK files here>

But when installed through sdkman, it is like this

jdk_root-folder/
            <actual jdk files>

In the work around method we need to imitate that file structure with Contents, Info.plist, Home, and then linking original jdk there.

But the method is little complicated and we have to repeat it for every single version.

It would be great if there is any easier way.

Thank you.

defaultacc
  • 13
  • 4

1 Answers1

1

The java_home script doesn't know about SDKman! Luckily, there is a built in home (or h) command like

sdk home java 17.0.2.fx-librca

outputs

/Users/efrisch/.sdkman/candidates/java/17.0.2.fx-librca

If you want the currently selected one, that's

$HOME/.sdkman/candidates/java/current
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I actually want to use this below line in my .zshrc file to change versions like this. ``alias java17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"`` to change jdk version but java_home is unable to locate SDKman jdk. – defaultacc Jul 24 '23 at 21:16
  • 1
    @defaultacc What part of my answer made you think I didn't know that? Again, you can't use `java_home` to do that. Because `java_home` doesn't know about sdkman. What you can do is use `sdk home`. ``alias java17="export JAVA_HOME=`sdk home java 17.0.2.fx-librca`"`` (or whatever java 17 you want). – Elliott Frisch Jul 24 '23 at 21:36
  • Sorry my bad, I didn't understand that you were implying me to use this ``alias java17="export JAVA_HOME=`sdk home java java 20.0.2-oracle`"`` instead of ``alias java17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version`` Thank You. – defaultacc Jul 24 '23 at 21:57
  • Well aside from that would appear to be `alias java20`, yes. – Elliott Frisch Jul 24 '23 at 22:08
  • Could you please let me know why these are not working. ```alias java8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`; java -version" alias java11="export JAVA_HOME=`/usr/libexec/java_home -v 11.0.19`; java -version" alias java17="export JAVA_HOME=`sdk home java 17.0.8-oracle` ; java --version" alias java20="export JAVA_HOME=`sdk home java 20.0.2-oracle` ; java --version"``` It is not changing my jdk version. – defaultacc Jul 24 '23 at 23:09
  • 1
    Because you are modifying `JAVA_HOME` only. You would also need to `export PATH=$JAVA_HOME/bin:$PATH` because you changed `JAVA_HOME`. This is entirely the purpose of the `sdk use` command. So you probably actually want `alias java20="sdk use java 20.0.2-oracle"` and you can then set `JAVA_HOME` to `$HOME/.sdkman/candidates/java/current` and be done with it. Just use `sdk use` to update "current". – Elliott Frisch Jul 24 '23 at 23:16
  • I was very confused about that, thank you for explaining it. Also what command should I use for java11 and java8 since they are not installed through sdk? – defaultacc Jul 24 '23 at 23:21
  • @defaultacc I would reinstall them through sdkman. Kind of the point of using a sdk manager. Let it manage your sdks. – Elliott Frisch Jul 24 '23 at 23:32
  • yes, but those versions are not available in sdk. – defaultacc Jul 24 '23 at 23:42
  • `sdk install java 8.0.372.fx-librca` and `sdk install java 11.0.20.fx-librca` for 8 and 11 respectively. – Elliott Frisch Jul 24 '23 at 23:50
  • `alias j17="sdk default java 17.0.8-oracle; java --version"` `alias j20="sdk default java 20.0.2-oracle;java --version"` I m using this now and it works fine. As for java 11 the version I need (openlogic jdk is not available in sdk so I can't use it). (I need openlogic jdk to run codereadystudio, for some reason it doesnt work without it). Anyways thanks for helping out. – defaultacc Jul 26 '23 at 04:45