Choose java version linux

Install and manage multiple Java versions on Linux using alternatives

Based on some comment suggestions I decided to extend the post to have a second version installed and show explicitly how to switch between them.

Lately I’ve been using Azul JVMs mostly so this time I will download the latest LTS Java 17 Open JDK from Azul

curl -L https://cdn.azul.com/zulu/bin/zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz > ~/tmp/zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz 

Cd in the folder where you downloaded it.

sudo mkdir /usr/lib/jvm/azul-open-jdk-17 
sudo tar -xzf ./zulu17.32.13-ca-jdk17.0.2-linux_x64.tar.gz -C /usr/lib/jvm/azul-open-jdk-17 --strip-components=1 
sudo update-alternatives --config java 

You will see a list of configured java in alternatives, the new one is not there so we need to register it like we did in the beginning of this article, let’s do it for java and javac, make sure to check the Selection number and add the new one to the next available, hit enter to continue with the current version before progressing, let’s now register the new one we downloaded.

Java versions installed alternatives

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/azul-open-jdk-17/bin/java 2 

The number in the end is the priority you can choose which one fits your goals better, by default the highest priority will be picked(lower number).

  • Repeat for javac, check the installed javac with sudo update-alternatives —config javac and then configure it with next available Reference.
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/azul-open-jdk-17/bin/javac 2 
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/azul-open-jdk-17/bin/jar 2 
sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/azul-open-jdk-17/bin/jshell 2 

Now that you have the new JDK installed and configured you can easily switch between versions using:

sudo update-alternatives --config java 

Pick the number of the version you want from the Selection list.

Type in the Reference number for the one you want to be used from the displayed list and check with java -version the same can be done for, javac, jshell, jar or any other tools you want to manage multiple versions using alternatives on linux.

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.

Читайте также:  Css all external links

Источник

How to change the default Java version on Ubuntu

If you are a Java developer, it is normal to have multiple Java versions installed on your machine to support different build environments. When a Java program is compiled, the build environment sets the oldest JRE version the program can support. Now, if you run this program on a Linux machine where an unsupported Java version is installed, you will encounter an exception.

For example, if your program is compiled on Java 11, it can’t be run on a machine where Java 8 is installed. But the good thing is you can install multiple Java versions on your machine and quickly change the default JRE version.

In this tutorial, I’ll explain how to change the default Java version on a Linux machine. First of all, run the following command to check the current Java version:

$ java -version openjdk version "1.8.0_191" OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.10.1-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode) 

As you can see above, the default Java version is currently set to OpenJDK JRE 1.8. Now, let’s run the following command to see all available Java versions:

$ sudo update-alternatives --config java 

Running the above command displays a list of installed Java JDKs and JREs allowing you to select the one as you want to set as default.

There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press enter> to keep the current choice[*], or type selection number: 

When prompted, select the Java version you would like to use. If the list does not include your desired Java version, you can always install it.

Now you can verify the default Java version as fellows:

$ java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.10.3) OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.10.3, mixed mode, sharing) 

That’s it. The default Java version is changed to OpenJDK 11.

If you frequently switch between different Java versions, it is a good idea to write a short script to automate the process. Here is the script I used for switching to OpenJDK 8 on my machine. java8.sh

sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/ export PATH=$PATH:$JAVA_HOME 

Similarly, you can create scripts for other Java versions installed on your machine. The next step is to add these scripts as aliases to .bashrc file.

... # Java Alias alias java8='source /opt/java/switch/java8.sh' alias java11='source /opt/java/switch/java11.sh' 

Read Next: How to install Java on Ubuntu 18.04 ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Switching between Java Versions on Ubuntu linux

If you’re using Ubuntu Linux on your daily basis work, you’ve probably Java installed on your machine. Personally I prefer using Wepupd8 PPA to manage JAVA installation, it makes my life a lot more easier especially for updates. The Wepupd8 team didn’t add any binary for Oracle JAVA installation and they made a script to download the Oracle JAVA from Oracle website and install it straight away. So whenever Oracle will release the update, I can simple upgrade via package manager.

Working with multiple Java versions in your machine is a normal thing, especially if you’re a Java developer, and because I’m a (very) lazy person, I’m always looking for a quicker/easier way to make the switch.

Today, I’ll share with you my tip on this subject. First, let’s run the following command:

$ sudo update-alternatives —config java

Running this command shows a list of installed Java JDKs and JREs allowing one to be selected as the default that is used when java needs to be executed.

There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press to keep the current choice[*], or type selection number: 

But I’m using it just to get the Installation path of each Java version.

Then, for each version I created a script that contain the following lines (in the example below, I’m showing the one for java 8):

sudo update-java-alternatives -s java-8-oracle export JAVA_HOME=/usr/lib/jvm/java-8-oracle/ export PATH=$PATH:$JAVA_HOME 

Note that I’m using u8.sh to make he switch to Java 8, and u9.sh for Java 9 and so on.
The final step is to add an alias in ~/.bashrc file to source our script as follow:

. # Alias alias u7='source /home/aboullaite/Utils/Java/u7.sh' alias u8='source /home/aboullaite/Utils/Java/u8.sh' alias u9='source /home/aboullaite/Utils/Java/u9.sh' 

and That’s all. Now for switching between Java versions, I only run u8, u7 or u9 😉

Источник

How to Change Java Version Linux

Working with different versions of Java is a common thing for developers. You may have different Java versions installed on your system and must switch between them when you want to work on a given project. Luckily, there is a quick way to change your system’s current Java version. We will cover all the details about changing your Java version in Linux.

Changing the Java Version in Linux

For this guide, we are working with Java 19. However, we install a lower version and check the steps to follow when you want to switch the version.

Let’s first check the current Java version in our system. For that, use the following command:

We verified that our current Java version is 19.0.1.

When you want to view your system’s installed Java JDKs and JREs, use the following command:

The command displays all the available Java versions. You can select which one to install from the listed options.

In our case, we only have one installed Java version. Notice that the following output doesn’t give any alternative Java versions.

Similarly, you can check the available Java versions using the update-alternatives command:

The output also displays the link to the currently set Java version. In our case, the link points to Java 19.

To demonstrate how we switch between the Java versions, let’s quickly install Java 19.

If we check the available Java versions, we now have Java 19 which is the currently set version, and an alternative version which is Java 17.

To change the Java version in Linux, use the –config java option with the update-alternatives command:

A list of all the available JDKs and JREs are displayed. Note the selection number of the Java version that you want to switch to. Next, enter its number in the confirmation prompt on the terminal and press the enter key.

In the example, we entered our selection as 1 to switch to Java 17. Once you enter the selection, you get a message which confirms that the selection is updated.

If we check the current Java version in our system, we should get the Java 17 as we managed to switch to it.

In the output, note that the current Java version is Java 17. If we run a program that requires Java 17, we wouldn’t get any errors.

That’s how you switch between Java versions in Linux. Feel free to try it out.

Conclusion

It’s common to have different Java versions installed on your system. Different programs may require different Java versions to support them. In such a case, you may have various Java versions on your system and you need to switch between them at different instances. We covered the simple steps that you should follow when changing the Java version on your Linux system. Hopefully, you are now at ease with conveniently working with different Java versions.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

Оцените статью