Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between different java versions in Linux?

I manually installed Java 8 and installed Java 11 via command line (open-jdk). I'm a noob at linux and don't know how to switch between the versions.

I ran the following commands to change the default version.

update-alternatives --config java

Returns:

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1101      auto mode
  1            /opt/java/jdk1.8.0_211/bin/java               1         manual mode
  2            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1101      manual mode

And

update-alternatives --config javac

Returns:

There is only one alternative in link group javac (providing /usr/bin/javac): /usr/lib/jvm/java-11-openjdk-amd64/bin/javac
Nothing to configure.
like image 313
Vandit Goel Avatar asked Jan 22 '20 09:01

Vandit Goel


1 Answers

You can use SDKMan which allow us to do the same, SDKMan is a free to use open-sourced tool which helps us to install and manage parallel versions of around 29 Software Development Kits such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.

And it also sets the _HOME and PATH environment variables automatically.

You can install SDKMan by running below commands

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

You can list all versions of Java using

$ sdk list java

And you can install different-different Java versions using sdk install command e.g.

$ sdk install java 9.0.4-open
$ sdk install java 11.0.2-open

And you can switch between them using sdk use and sdk default commands, You can read more about it on my blog How to Install Multiple Versions of Java on the Same Machine.

like image 166
Naresh Joshi Avatar answered Oct 05 '22 14:10

Naresh Joshi