Search code examples
javasdkman

Install JDK15 for MacBook with Apple silicon and manage multiple versions


There is a related question but I have two additional specific requirements:

  • I need JDK 15 (which seems to be a rare breed).
  • I want to manage multiple versions, ideally with SDKMAN, since I already used it to install JDK17.

In the past, I've managed multiple Java versions with SDKMAN!, from external sources, including brew (Homebrew), if they were not available with SDKMAN, and manually copied them to the expected SDKMAN directory.

Since JDK 15 is neither available with SDKMAN or brew, I downloaded jdk-15.0.2_osx-x64_bin.dmg from the Oracle Java Archive (had to create an account for that) and installed it.

The challenge is, it won't let me choose the destination, the only option is to "install it for all users" and it spreads the files all over the system, the executables under /usr/bin. This makes it very hard to manage multiple Java versions.

Nonetheless I tried the following:

$ mkdir -p ~/.sdkman/candidates/java/15.0.2-oracle/bin/
$ cp /usr/bin/ja* ~/.sdkman/candidates/java/15.0.2-oracle/bin/
$ sdk use java 15.0.2-oracle

Using java version 15.0.2-oracle in this shell.

It doesn't work though:

$ java --version
Killed: 9

This is no surprise, since there are many other files missing. Wondering if someone already managed to hack a solution for this. Not necessarily with the same approach, any alternative is fine, provided it meets both requirements. Thanks.


Solution

  • Just found out that the Oracle installer did follow the tradition of keeping the JDK in its own directory, in this case, it was /Library/Java/JavaVirtualMachines/.

    Knowing this, the rest was easy, and I can now manage JDK 15 with SDKMAN, along with JDK 17 (and other future versions) which was installed with sdk install java 17.0.12-oracle. Let bash do the talking:

    $ mkdir ~/.sdkman/candidates/java/15.0.2-oracle
    
    $ cp -r /Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/ \
         ~/.sdkman/candidates/java/15.0.2-oracle/
    
    $ ls ~/.sdkman/candidates/java/15.0.2-oracle/
    bin     conf    include jmods   legal   lib     man     release
    
    $ sdk use java 15.0.2-oracle
    Using java version 15.0.2-oracle in this shell.
    
    $ java --version
    java 15.0.2 2021-01-19
    Java(TM) SE Runtime Environment (build 15.0.2+7-27)
    Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)
    
    $ sdk use java 17.0.12-oracle 
    
    Using java version 17.0.12-oracle in this shell.
    
    $ java --version
    java 17.0.12 2024-07-16 LTS
    Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286)
    Java HotSpot(TM) 64-Bit Server VM (build 17.0.12+8-LTS-286, mixed mode, sharing)
    

    Of course, I don't need to tell you that if you have a choice, use a more recent JDK!