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-alter
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 :
Install every java version in /opt/java/
, 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
)
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 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