How to Install Multiple Versions of Java on the Same Machine
Some time back I have written one articleJava Lambda Expression Explained with Example but it was easy for me to explore and Java 8 because I was using it in my project and I was allowed to install and use it. But in my current project, we are still using Java 8 and now I want to upgrade myself and learn Java 11 but can not install it.
And this happens with a lot of Java developers, Sometimes you want to learn and explore latest version of Java e.g. Java 11 but you can not install it on your machine because you and your team is working on some older version e.g. Java 8, and you do not want to break your project.
Or suppose you are working on multiple projects some of which use Java 8 and some new projects are using Java 11. So in order to work on these projects parallelly, you will need to install multiple JDK on your machine and you should be able to switch between them.
What if there is a way and what if you are enabled to install multiple versions of Java and turn off and turn on them according to your need.
Well there is a tool calledSDKMan which allow us to do the same and according to the official website:
SDKMan! is a tool for managing parallel versions of multiple
Software 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 following:
- SDKMan is free to use and it is developed by the open source community.
- SDKMan is written in bash and it only requires curl and zip/unzip programs to be present on your system.
- SDKMan can install around 29 Software Development Kits for the JVM such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.
- We do not need to worry about setting the
_HOMEandPATHenvironment variables because SDKMan handles it 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 following commands:
Simply open a new terminal and enter:
1 2 | $ source "$HOME/.sdkman/bin/sdkman-init.sh" |
We can verify the installation using sdk version and sdk help command will us complete help about the usage of sdk command.
Because SDKMan is written in bash and only require 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 above commands.
Installing Java Using SDKMan
SDKMan supports installations of 29 Software Development Kits for the JVM e.g. Java, Groovy, Scala, Kotlin and Ceylon, Grails, SBT, Spark, Spring Boot. We can get the complete list usingsdk 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.
Commandsdk list java will give us a list of java versions which we can install using SDKMan like below:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $ 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 version of Java v9 then we will execute below command in our terminal which will also update the PATH and JAVA_HOME variables:
1 | $ 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 | Downloading: java 9.0.4-openIn progress...######################################################################## 100.0%Repackaging Java 9.0.4-open...Done repackaging...Cleaning up residual files...Installing: java 9.0.4-openDone installing!Setting java 9.0.4-open as default. |
Now if we check our Java version and JAVA_HOME, we can see Java has been updated to 9.0.4
1 2 3 4 5 6 7 | $ java -versionopenjdk 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 we want to install Java V11 version then we can execute the command:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | $ sdk install java 11.0.2-openDownloading: java 11.0.2-openIn progress...######################################################################## 100.0%Repackaging Java 11.0.2-open...Done repackaging...Cleaning up residual files...Installing: java 11.0.2-openDone installing!Do you want java 11.0.2-open to be set as default? (Y/n): YSetting java 11.0.2-open as default. |
Now if we again check our Java version and JAVA_HOME, we can see Java has been updated to 11.0.2:
1 2 3 4 5 6 7 | $ java -versionopenjdk version "11.0.2" 2019-01-15OpenJDK 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 require to close and reopen your terminal in order to see the changes.
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 by<java-folder>.
The second thing we want to do is to set up a symbolic link between our installed<java-folder> and SDKMan, we can do it by running below commands:
1 2 3 | $ 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 executesdk list java command, we will get:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ================================================================================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 3 JDK installed on our machine and JDK11.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 usingsdk use java <version_which_you_want_to_use> like the following:
Using Java 9
1 2 3 4 5 6 7 | $ sdk use java 9.0.4-openUsing java version 9.0.4-open in this shell.$ java -versionopenjdk 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
1 2 3 4 5 6 7 | $ sdk use java jdk1.8.0_172.jdkUsing java version jdk1.8.0_172.jdk in this shell.$ java -versionjava 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
1 2 3 4 5 6 7 | $ sdk use java 11.0.2-openUsing java version 11.0.2-open in this shell.$ java -versionopenjdk version "11.0.2" 2019-01-15OpenJDK Runtime Environment 18.9 (build 11.0.2+9)OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode) |
Using sdk use command will only enable you to use a specified version of 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 JDK for all terminals and applications, you can use the command sdk default java <your_version>
1 2 3 4 5 6 7 | $ sdk default java 11.0.2-openDefault java version set to 11.0.2-open$ java -versionopenjdk version "11.0.2" 2019-01-15OpenJDK 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:
1 | $ 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).
01 02 03 04 05 06 07 08 09 10 | $ sdk install java 11.0.2-openFound a previously downloaded java 11.0.2-open archive. Not downloading it again...Installing: java 11.0.2-openDone installing!Do you want java 11.0.2-open to be set as default? (Y/n): ySetting java 11.0.2-open as default. |
Using SDMan installed JDK from IntelliJ IDEA
SDKMan installs all JDK under .sdkman/candidates/java/ folder and you can find.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 onNew... under Project SDK section and there you can enter .sdkman/candidates/java/ in browse window and can select any JDK from.sdkman/candidates/java/.
Because.sdkman is a hidden folder and in case you face some problem in finding it, you can always create a symbolic link to it with a non-hidden folder using below command.
1 | $ ln -s ~/.sdkman ~/sdkman |
You can find the complete source code for my articles on thisGithub Repository and please feel free to provide your valuable feedback.
Published on Java Code Geeks with permission by Naresh Joshi, partner at our JCG program. See the original article here: How to Install Multiple Versions of Java on the Same Machine Opinions expressed by Java Code Geeks contributors are their own. |

