{{announcement.body}}
{{announcement.title}}

How to Install Multiple Versions of Java on the Same Machine

DZone 's Guide to

How to Install Multiple Versions of Java on the Same Machine

Need help adding the latest version of the JDK to your machine?

· Java Zone ·
Free Resource

Some time back, I wrote the article Java Lambda Expression Explained with Example, but it was easy for me to explore Java 8 because I was using it in my project and allowed to install it. But in my current project, we are still using Java 8, and now, I want to upgrade and learn Java 11, but unfortunately, I cannot install it.

And this happens a lot with Java developers. Sometimes, you want to learn and explore the latest version of Java, but you cannot install it on your machine because you and your team are working on an older version, e.g. Java 8, and you don't want to accidentally break your project.

Or, suppose you are working on multiple projects, some of which use Java 8 and some of which are new projects using Java 11. So, in order to work on both of these projects, you will need to install multiple versions of the JDK on your machine and be able to switch between them.

What if there is a way to install multiple versions of Java and turn different versions on and off, depending on your needs.

Well, there is a tool called SDKMan which allows us to do just that. According to their website:

SDKMan! is a tool for managing parallel versions of multiple S oftware Development Kits on most Unix-based systems. It provides a convenient Command Line Interface (CLI) and API for installing, switching, removing, and listing Candidates.

Some points about SDKMan are as follows:

  1. SDKMan is free to use and is developed by the open-source community.
  2. SDKMan is written in bash and only requires curl and zip/unzip programs to be present on your system.
  3. We do not need to worry about setting the _HOME and PATH environment variables because SDKMan handles them automatically.

Installing SDKMan

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"


We can verify the installation using sdk version and sdk help commands, which will give us complete help with the usage of the sdk command.

Because SDKMan is written in bash and only requires curl and zip/unzip to be present on your system, you can install SDKMan on Windows as well either by first installing Cygwin or Git Bash for Windows environment and then running the above commands.

Installing Java Using SDKMan

SDKMan supports installations of 29 Software Development Kits for the JVM, e.g. Java, Groovy, Scala, Kotlin, Ceylon, Grails, SBT, Spark, Spring Boot. We can get the complete list using the sdk list command.

SDKMan also helps us install build tools such as Ant, Maven, and Gradle. You can read more about these build tools on Java Build Tools Comparisons: Ant vs Maven vs Gradle.

Command sdk list java will give us a list of Java versions that we can install using SDKMan, like below:

$ sdk list java

================================================================================
Available Java Versions
================================================================================
     13.ea.02-open       1.0.0-rc-10-grl                                        
     12.ea.26-open       1.0.0-rc-9-grl                                         
     11.0.2-zulu         1.0.0-rc-8-grl                                         
     11.0.2-open                                                                
     11.0.1-zulufx                                                              
     10.0.2-zulu                                                                
     10.0.2-open                                                                
     9.0.7-zulu                                                                 
     9.0.4-open                                                                 
     8.0.202-amzn                                                               
     8.0.201-zulu                                                               
     8.0.201-oracle                                                             
     8.0.192-zulufx                                                             
     7.0.181-zulu                                                               
     1.0.0-rc-11-grl                                                            
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================


Now, suppose we want to install OpenJDK versions of Java v9, then we will execute the command below in our terminal, which will also update the PATH and  JAVA_HOME variables:

$ sdk install java 9.0.4-open


The command will take some time to execute because it will be downloading the JDK on our machine and the command will output something like:

Downloading: java 9.0.4-open
In progress...
######################################################################## 100.0%

Repackaging Java 9.0.4-open...

Done repackaging...
Cleaning up residual files...

Installing: java 9.0.4-open
Done installing!

Setting java 9.0.4-open as default.


Now, if we check our Java version and  JAVA_HOME, we can see that Java has been updated to 9.0.4

$ java -version
openjdk version "9.0.4"
OpenJDK Runtime Environment (build 9.0.4+11)
OpenJDK 64-Bit Server VM (build 9.0.4+11, mixed mode)

$ echo $JAVA_HOME
/Users/xbbnrde/.sdkman/candidates/java/current


We can follow the same process to install any version, which we can see in the above list. Let us suppose that we want to install Java V11 version, then we can execute the command:

$ sdk install java 11.0.2-open
Downloading: java 11.0.2-open
In progress...
######################################################################## 100.0%

Repackaging Java 11.0.2-open...

Done repackaging...
Cleaning up residual files...

Installing: java 11.0.2-open
Done installing!

Do you want java 11.0.2-open to be set as default? (Y/n): Y

Setting java 11.0.2-open as default.


Now, if we again check our Java version and JAVA_HOME , we can see that Java has been updated to 11.0.2:

$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

$ echo $JAVA_HOME
/Users/xbbnrde/.sdkman/candidates/java/current


Note: some times, you will be required to close and reopen your terminal in order to see the changes.

sdkman-installing-and-running-multiple-java-versions-java11-and-java-9-simultaneously

Pointing SDKMan to Your Existing Installed Java Version

First, we need to find out where on your machine Java is installed. On my machine, it is installed in the folder jdk1.8.0_172.jdk, which lies under the folder /Library/Java/JavaVirtualMachines ; let's refer to this folder as <java-folder>.

The second thing we want to do is to set up a symbolic link between our installed <java-folder> and SDKMa; we can do it by running the below commands:

$ ln -s /Library/Java/JavaVirtualMachines/<java-folder>  ~/.sdkman/candidates/java/<java-folder>

$ sudo ln -s /Library/Java/JavaVirtualMachines/<java-folder>/Contents/Home/bin  ~/.sdkman/candidates/java/<java-folder>/bin


Now, if we again execute the sdk list java command, we will get:

================================================================================
Available Java Versions
================================================================================
   + jdk1.8.0_172.jd     8.0.201-zulu                                           
     13.ea.02-open       8.0.201-oracle                                         
     12.ea.26-open       8.0.192-zulufx                                         
     11.0.2-zulu         7.0.181-zulu                                           
 > * 11.0.2-open         1.0.0-rc-12-grl                                        
     11.0.2.j9-adpt      1.0.0-rc-11-grl                                        
     11.0.2.hs-adpt      1.0.0-rc-10-grl                                        
     11.0.1-zulufx       1.0.0-rc-9-grl                                         
     10.0.2-zulu         1.0.0-rc-8-grl                                         
     10.0.2-open                                                                
     9.0.7-zulu                                                                 
   * 9.0.4-open                                                                 
     8.0.202-amzn                                                               
     8.0.202.j9-adpt                                                            
     8.0.202.hs-adpt                                                            

================================================================================
+ - local version
* - installed
> - currently in use
================================================================================


We can clearly see that we have three JDKs installed on our machine and JDK 11.0.2-open is in use as of now. However, if we want to switch between them or install a new one, we can do that very easily by using sdk use java <version_which_you_want_to_use> like the following:

Using Java 9

$ sdk use java 9.0.4-open
Using java version 9.0.4-open in this shell.

$ java -version
openjdk version "9.0.4"
OpenJDK Runtime Environment (build 9.0.4+11)
OpenJDK 64-Bit Server VM (build 9.0.4+11, mixed mode)


Using Locally Installed Java 8

$ sdk use java jdk1.8.0_172.jdk
Using java version jdk1.8.0_172.jdk in this shell.

$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)


Using Java 11

$ sdk use java 11.0.2-open
Using java version 11.0.2-open in this shell.

$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)


Using the  sdk use command will only enable you to use a specified version of the JDK in the same terminal shell where you are executing the sdk use command. And if you close the terminal and open it again, you will be on the previously installed version.

But if you want to activate one version of the JDK for all terminals and applications, you can use the command sdk default java <your_version>.

$ sdk default java 11.0.2-open
Default java version set to 11.0.2-open

$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)


Or, you can uninstall that version and install it again, like below, which will set that version to the current JDK.

Uninstalling a JDK Version

In case you want to uninstall any JDK version, e.g. 11.0.2-open , you can do that as follows:

$ sdk uninstall java 11.0.2-open


And in case you want to install the uninstalled version back again, you can install it again, and this time, SDKMan will not download it because it already has that on your machine (unless you do not delete that manually).

$ sdk install java 11.0.2-open

Found a previously downloaded java 11.0.2-open archive. Not downloading it again...

Installing: java 11.0.2-open
Done installing!

Do you want java 11.0.2-open to be set as default? (Y/n): y

Setting java 11.0.2-open as default.


Using the SDKMan-Installed JDK From IntelliJ IDEA

SDKMan installs all JDKs under the .sdkman/candidates/java/ folder, and you can find the .sdkman folder in your home directory.

After opening any Java project in IntelliJ, you can press Command + : to open the project structure window. In that window, you will need to click on New... under Project SDK section, and there, you can enter .sdkman/candidates/java/ in the browser window and select any JDK from .sdkman/candidates/java/.

Because .sdkman is a hidden folder, and in case you face some problems finding it, you can always create a symbolic link to it with a non-hidden folder using the command below:

$ ln -s ~/.sdkman ~/sdkman


You can find the complete source code for my articles on this GitHub repository, and please feel free to provide your valuable feedback.

Topics:
java ,jvm ,versions ,java 9 ,java 10 ,java 11 ,jdk ,jdk 11

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}