DEV Community

Marcos Maia
Marcos Maia

Posted on

Install and manage multiple Java versions on Linux using alternatives

On this post, I will guide you on installing Java on your development Linux machine. I decided to do this post after getting some questions on how do I manage multiple java versions in my Development environments if I use something to manage it like Sdkman, which I don't, in this post I will explain why.

Being in this industry for over 20 years I have developed software and scripts in many different languages like JavaScript, Pascal, Go, Python and others but I am mainly a passionate Java developer and I am committed to helping other colleagues to start programming in Java and help to demystify the fallacy that Java is complex or difficult to start. Java is the most used programming language for developing complex and enterprise software and it has by far the better ecosystem with it's available libraries, IDEs and tooling.

My preferred development environment is Linux so I'd rather use Linux alternatives to manage java SDK installations as it's built-in in Linux and allow you to manage not only java but any other binaries you want to manage and make accessible in your command line when using Linux. I will guide you to the process of installing Java 11 and running your first Hello World application using it.

The full installation process will be using the command line. So let's start, open a terminal console and cd to your preferred working directory.

  • Make sure to have wget installed.
sudo apt update && sudo apt install wget
Enter fullscreen mode Exit fullscreen mode
  • Download openjdk:
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
Enter fullscreen mode Exit fullscreen mode

Can check for updated java 11 versions here: https://jdk.java.net/java-se-ri/11

  • Once download finished, add permissions:
chmod +x openjdk-11+28_linux-x64_bin.tar.gz
Enter fullscreen mode Exit fullscreen mode
  • Create folder where jdk will be installed
mkdir /usr/lib/jvm/open-jdk-11
Enter fullscreen mode Exit fullscreen mode
  • Extract it to /usr/lib/jvm/open-jdk-11 folder you have just created.
tar -xzf ./openjdk-11+28_linux-x64_bin.tar.gz -C /usr/lib/jvm/open-jdk-11 --strip-components=1
Enter fullscreen mode Exit fullscreen mode
  • Update alternatives to add java, javac, jshell and jar

    • list installed version
update-alternatives --list java
Enter fullscreen mode Exit fullscreen mode
  • configure java installation
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/open-jdk-11/bin/java 1
Enter fullscreen mode Exit fullscreen mode
  • configure javac installation
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/open-jdk-11/bin/javac 1
Enter fullscreen mode Exit fullscreen mode
  • configure jar installation
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/open-jdk-11/bin/jar 1
Enter fullscreen mode Exit fullscreen mode
  • configure jshell installation
sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/open-jdk-11/bin/jshell 1
Enter fullscreen mode Exit fullscreen mode
  • Test installation, get java version:
java -version
Enter fullscreen mode Exit fullscreen mode
  • Test installation using jshell
jshell
Enter fullscreen mode Exit fullscreen mode
  • Type in
System.out.println("Hello World");
Enter fullscreen mode Exit fullscreen mode
  • Hit Ctrl + D to exit.

If you have multiple versions of java installed using this same process above, you can just switch between them using alternatives,

  • Display installed versions of java
update-alternatives --display java
Enter fullscreen mode Exit fullscreen mode
  • Config the version you want to use:
sudo update-alternatives --config java
Enter fullscreen mode Exit fullscreen mode

You can now pick the option you want from the available list. That's it, you're done and have a working local development java environment ready to go.

If you want to quick start with creating an API in Java using Spring Boot, make sure you have git and maven installed(sudo apt install git && sudo apt install maven) and check out this Spring Boot Crash Course, it's quite easy and quick to follow.

Discussion (11)

Collapse
samprogramiz profile image
Samuel Owino πŸ‡°πŸ‡ͺ πŸŒπŸ“±πŸ‘¨πŸΏβ€πŸ’»πŸ”­πŸŒŒ • Edited on

If you installed and configured an additional version in the guides, could have been super helpful because that's the whole point - "working with multiple versions of java", otherwise it's just another "How to install java in Linux - Tutorial"

Still forced to just work with /etc/environment | $PATH update

Collapse
katarman profile image
katarman

Hello thank for the article. How can I have multiple versions of JDK and Java ?
All the Linux distro have open-jdk in /etc/alternatives. Can I have jdk 11 and jdk 8 tohether?
Also I want to have the JDK 8 in /usr and a PATH variable java_home or java8 in system varibles, but I understand that the only way is to add variable in .bashrc.
Can you make it clear ?
Thank you in advance

Collapse
imadoor profile image
imadoor

Just what I was looking for. Thanks for sharing!

Collapse
thegroo profile image
Marcos Maia Author

I am glad it helped.

Collapse
hectorhugocc profile image
Hector Hugo

I have a problem. Using the 'echo $ PATH' command returns me a value but when reviewing the 'etc/environment' file you see another value. The same goes for the JAVA_HOME variable. Could you tell me what it is? Thanks for the support.

Collapse
thegroo profile image
Marcos Maia Author

Hum.... most likely you have in one of these files under your $HOME folder: .bash_profile, .bashrc, .zshrc or similar a configuration setting the $JAVA_HOME specifically to an existing installation? If you have I would recommend you to remove the entry and source the file or restart / login again to see if it's gone. In most cases you don't really need a $JAVA_HOME hardcoded in your profiles(with some exceptions).

If you type only env in your terminal you should see all existent variables in your machine.

Collapse
binarydiver profile image
Kururu

It was good working!
Thanks a lot.

Collapse
punitkulal profile image
Punit Kulal

Great, helped me learn about alternatives in linux as well

Collapse
sachajw profile image
Sacha

This is amazing! Thank you Marcos for this post!

Collapse
hophiducanh profile image
Ho Anh

I've just added more details. Check out here: github.com/logbasex/coding-notes/b...

Collapse
varunraje profile image
Varun Deshpande

How to configure open jdk 8 with this?