DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Evolving Domain-Specific Languages
  • The First Annual Recap From JPA Buddy
  • Is Java Still Relevant?
  • Step By Step Guide To Using Mule ESB

Trending

  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Start Coding With Google Cloud Workstations
  1. DZone
  2. Coding
  3. Java
  4. How To Install Multiple Versions of Java on the Same Machine

How To Install Multiple Versions of Java on the Same Machine

SDKMan! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-based systems. Read more!

By 
Naresh Joshi user avatar
Naresh Joshi
DZone Core CORE ·
Updated Sep. 07, 20 · Tutorial
Likes (24)
Comment
Save
Tweet
Share
179.6K Views

Join the DZone community and get the full member experience.

Join For Free

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 was 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 that allows us to do just that. According to their 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 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 platform, such as Mac OSX, Linux, Cygwin, Solaris, and FreeBSD, and we can install it using the following commands:

Shell
 
$ 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, and 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 the 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 this:

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 the Java V11 version, 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.

Java (programming language) Machine Command (computing) intellij Java Development Kit Software development

Published at DZone with permission of Naresh Joshi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Evolving Domain-Specific Languages
  • The First Annual Recap From JPA Buddy
  • Is Java Still Relevant?
  • Step By Step Guide To Using Mule ESB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!