Managing multiple Java JDKs on single system?

I’m not a java developer but recently I had the need to manage multiple Java JDKs on a single system. Because I had to keep running an older java version for compatibility. Upgrading my local Java JDK wasn’t the ideal solution. For my case and that’s where SDKMan came in – a JAVA JDK Manager that will make you life easier.

Turns out SDKMan is very similar to NVM  (node.js version manager) that I use regularly.

SDKMan can run on any UNIX-based platforms such as Mac OSX, Linux, Cygwin, Solaris, and FreeBSD, and we can install it using the following commands:

$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"

If the install went the way it should, by entering the command “$ sdk” you should see something like this:

Usage: sdk <command> [candidate] [version]
       sdk offline <enable|disable>

   commands:
       install   or i    <candidate> [version] [local-path]
       uninstall or rm   <candidate> <version>
       list      or ls   [candidate]
       use       or u    <candidate> <version>
       default   or d    <candidate> [version]
       home      or h    <candidate> <version>
       env       or e    [init]
       current   or c    [candidate]
       upgrade   or ug   [candidate]
       version   or v
       broadcast or b
       help
       offline           [enable|disable]
       selfupdate        [force]
       update
       flush             <broadcast|archives|temp>

   candidate  :  the SDK to install: groovy, scala, grails, gradle, kotlin, etc.
                 use list command for comprehensive list of candidates
                 eg: $ sdk list
   version    :  where optional, defaults to latest stable if not provided
                 eg: $ sdk install groovy
   local-path :  optional path to an existing local installation
                 eg: $ sdk install groovy 2.4.13-local /opt/groovy-2.4.13

To help you getting started managing multiple Java JDKS, here’s a list of essential commands such as switching JDK version, install new JDK versions, and linking already installed JDKS version with SDKMan.

List available java JDK
$ sdk list java        


Install a particular JDK
$ sdk install java 10.0.2-open
      
      Note: 10.0.2-open matches the Identifier from results comming from sdk list java
      The command might take a few minutes. Be patient. 

Activating a particular JDK
$ sdk use java 10.0.2-open
      Replace 10.0.2-open by any of the identifiers you have installed
 
Linking pre-existing JDKS
If you like me have already installed jdks you can link it to sdk

sdk installs binaires at ~/.sdk
java by default installs jdks at /Library/Java/JavaVirtualMachines
to make sure that sdkman is aware of those previously installed vms we have to do the following for each vm:

     $ ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/ ~/.sdkman/candidates/java/jdk1.8.0_92.jdk
     $ ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/ ~/.sdkman/candidates/java/jdk1.7.0_79.jdk

     Note: jdk1.8.0_92.jdk is one of my pre-existing VM's

     Once the linking is completed, if you re-run "sdk list java" you will see a section named "Unclassified"
     In my case:
     Unclassified  |     | jdk1.8.0_92.jdk | none    | local only | jdk1.8.0_92.jdk

$ sdk use java {identifer}
    Every time you switch java, $JAVA_HOME is automatically updated. 

Hope you find this post useful. Make sure to check all other posts here.

See you soon.

Márcio

Leave a Reply

Close Menu