127

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?

Aaron
  • 55,518
  • 11
  • 116
  • 132
user1680916
  • 1,273
  • 2
  • 9
  • 5

11 Answers11

68

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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Jordan Denison
  • 2,649
  • 14
  • 14
66

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.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 1
    How do I update this automatically? I just get tired of having to manually configure it every time when calling `sudo update-alternatives --config javac` – Rock Mar 28 '19 at 19:06
  • In the past I would just update one directory (say `/usr/java/latest`) to point to the java version I wanted to use, and put `/usr/java/latest/bin` on my path so I only needed to do one update to get javac, java, javap, etc (all in one change). – PatS Oct 17 '22 at 20:06
43

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
jopasserat
  • 5,721
  • 4
  • 31
  • 50
  • Note: it will print "no alternatives for mozilla-javaplugin.so" but it can be ignored since the commands get updated anyways. – Nand Oct 15 '22 at 14:06
25

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
user2733648
  • 350
  • 3
  • 4
13

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

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18
  • 1
    Love this technique ! A couple of things missing from the description: You need to create `/opt/java` and you also need to add `/opt/java/current/bin` to your $PATH – David Farrell Aug 28 '21 at 22:52
9

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.

Paul-Sebastian Manole
  • 2,538
  • 1
  • 32
  • 33
8

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

sudo update-alternatives --config java

Command line screenshot

Md. Shahariar Hossen
  • 1,367
  • 10
  • 11
  • This changes only java link, but in JDK there are many more tools like javac, javap, keytool, jpackage etc. Use update-java-alternatives script for this, that handles all tools in JDK. But as I see it, it's unnecessarily complicated – all the JDK command should be soft linked to another soft link, that actually points to real jdk, like javac -> /usr/lib/jvm/current-jvm/bin/javac and current-jvm -> /usr/lib/jv/java-11-xyz. This way it suffices to just change one link to change whole JDK. – Cromax May 04 '23 at 22:49
4

Steps I followed to install java 17 in Debian 10

Step 1: Download jdk from URL and move to server https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb

Alternatively you can download directly in server using weget command

 sudo apt update
 sudo apt -y install wget curl
 wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb

Step 2: installed using command '

  sudo apt install ./jdk-17_linux-x64_bin.deb

step 3: Update default javac and java version as 17

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17/bin/javac 1
sudo update-alternatives --config javac

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
sudo update-alternatives --config java

Step 4: Check jre version

java -version

Check jdk version

   javac -version
Radhakrishnan
  • 564
  • 4
  • 6
3

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

You can install additional tools like maven with sdkman, too.

1

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

hinnarksen
  • 11
  • 1
0

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

Ajay
  • 176
  • 6