How to use the command update-alternatives --config java

JavaDebian

Java Problem Overview


I am installing Apache Solr on Linux Debian (Squeeze). I have been instructed to install sun-java jdk 1st. Then am told that I should use the command sudo update-alternatives --config java to make sure that a particular java (sun-java) is the default runtime. Yet when I run this command I get:

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                      Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      auto mode
  1            /usr/bin/gij-4.4                           1044      manual mode
  2            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
  3            /usr/lib/jvm/java-6-sun/jre/bin/java       63        manual mode

Press enter to keep the current choice[*], or type selection number:

So which number should I select to have sun-java as the default runtime? Also, what exactly do th auto and manual modes mean? If I select 3 above and run the update-alternative command I receive the same output as above, again nothing seems to have changed except that the * is now in front of the 3, though it still reads manual mode.

Is there a way to confirm that what I have done is correct?

Java Solutions


Solution 1 - Java

You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:

> When using the --config option, alternatives will list all of the > choices for the link group of which given name is the master link. You > will then be prompted for which of the choices to use for the link > group. Once you make a change, the link group will no longer be in > auto mode. You will need to use the --auto option in order to return > to the automatic state.

And I believe auto mode is set when you install the first/only JRE/JDK.

Solution 2 - Java

Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:

  1. Install the alternative for javac

     $ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1
    
  2. Check / update the alternatives config:

     $ sudo update-alternatives --config javac
    

If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.

To check everything is setup correctly then:

$ which javac
/usr/bin/javac

$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  4 17:10 /usr/bin/javac -> /etc/alternatives/javac

$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep  4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac

And finally

$ javac -version
javac 1.8.0_144

Repeat for java, keytool, jar, etc as needed.

Solution 3 - Java

update-alternatives is problematic in this case as it forces you to update all the elements depending on the JDK.

For this specific purpose, the package java-common contains a tool called update-java-alternatives.

It's straightforward to use it. First list the JDK installs available on your machine:

root@mylaptop:~# update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

And then pick one up:

root@mylaptop:~# update-java-alternatives -s java-1.7.0-openjdk-amd64

Solution 4 - Java

This is how I install jdk

#!/bin/bash
cd /opt/
sudo mkdir java
sudo tar -zxvf ~/Downloads/jdk-8u192-linux-x64.tar.gz
sudo ln -s  jdk1.8.0_192 current
for file in /opt/java/current/bin/*
do
   if [ -x $file ]
   then
      filename=`basename $file`
      sudo update-alternatives --install /usr/bin/$filename $filename $file 20000
      sudo update-alternatives --set $filename $file
      #echo $file $filename
   fi
done

Solution 5 - Java

I'm using Ubuntu 18.04 LTS. Most of the time, when I change my java version, I also want to use the same javac version.
I use update-alternatives this way, using a java_home alternative instead :

Installation

Install every java version in /opt/java/<version>, for example

~$ ll /opt/java/
total 24
drwxr-xr-x 6 root root 4096 jan. 22 21:14 ./
drwxr-xr-x 9 root root 4096 feb.  7 13:40 ../
drwxr-xr-x  8 stephanecodes stephanecodes 4096 jan.   8  2019 jdk-11.0.2/
drwxr-xr-x  7 stephanecodes stephanecodes 4096 dec.  15  2018 jdk1.8.0_201/

Configure alternatives

~$ sudo update-alternatives --install /opt/java/current java_home /opt/java/jdk-11.0.2/ 100
~$ sudo update-alternatives --install /opt/java/current java_home /opt/java/jdk1.8.0_201 200

Declare JAVA_HOME (In this case, I use a global initialization script for this)

~$ sudo sh -c 'echo export JAVA_HOME=\"/opt/java/current\" >> environment.sh'

Log Out or restart Ubuntu (this will reload /etc/profile.d/environment.sh)

Usage

Change java version

Choose the version you want to use

~$ sudo update-alternatives --config java_home

There are 2 choices for the alternative java_home (providing /opt/java/current).

  Selection    Path                    Priority   Status
------------------------------------------------------------
  0            /opt/java/jdk-11.0.2     200       auto mode
  1            /opt/java/jdk-11.0.2     200       manual mode
* 2            /opt/java/jdk1.8.0_201   100       manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Check version

~$ 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)

~$ javac -version
javac 11.0.2

Tip

Add the following line to ~/.bash_aliases file :

alias change-java-version="sudo update-alternatives --config java_home && java -version && javac -version"

Now use the change-java-version command to change java version

Solution 6 - Java

There are many other binaries that need to be linked so I think it's much better to try something like sudo update-alternatives --all and choosing the right alternatives for everything else besides java and javac.

Solution 7 - Java

If you want to switch the jdk on a regular basis (or update to a new one once it is released), it's very conveniant to use sdkman.

You can additional tools like maven with sdkman, too.

Solution 8 - Java

Have a look at https://wiki.debian.org/JavaPackage At the bottom of this page an other method is descibed using a command from the java-common package

Solution 9 - Java

After installing any open jdk simply run and select your desired jdk:

sudo update-alternatives --config java

Command line screenshot

Solution 10 - Java

I have 2 versions of java Installed on my AWS EC2 instance; java 8 & 11 but couponservice-0.0.1-SNAPSHOT.jar file needs java 11 to run because this .jar file was created using java 11.

[root@ip-172-31-94-132 ~]# alternatives --config java

There are 2 programs which provide 'java'.

Selection Command

  • 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.amzn2.0.1.x86_64/jre/bin/java)
  • 2 /usr/java/jdk-11.0.12/bin/java

Enter to keep the current selection[+], or type selection number: 2

[root@ip-172-31-94-132 ~]# java -version

java version "11.0.12" 2021-07-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)

Now you can run .jar file specific to

[root@ip-172-31-94-132 ~]# java -jar couponservice-0.0.1-SNAPSHOT.jar

Reference:- https://www.server-world.info/en/note?os=CentOS_7&p=jdk11&f=2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1680916View Question on Stackoverflow
Solution 1 - JavaJordan DenisonView Answer on Stackoverflow
Solution 2 - JavaRichard GrimshawView Answer on Stackoverflow
Solution 3 - JavajopasseratView Answer on Stackoverflow
Solution 4 - Javauser2733648View Answer on Stackoverflow
Solution 5 - JavaStephane JanicaudView Answer on Stackoverflow
Solution 6 - JavaPaul-SebastianView Answer on Stackoverflow
Solution 7 - Javaarmadill013View Answer on Stackoverflow
Solution 8 - JavahinnarksenView Answer on Stackoverflow
Solution 9 - JavaMd. Shahariar HossenView Answer on Stackoverflow
Solution 10 - JavaAjayView Answer on Stackoverflow