There are 2 instances of java version on my VM and I want to force to use java "java-1.8.0-openjdk" using shell script.
# sudo 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.312.b07-2.el8_5.x86_64/jre/bin/java)
+ 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java)
I tried below :
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME
java -version
o/p: openjdk version "11.0.13" 2021-10-19 LTS
Help here would be really appreciated.
2
Answers
Suggesting to try again with this:
The difference in the the 3rd line. Added
/bin
to path.If not working try (thanks to comment from @Jeff Schallerr):
In your question you specify a very specific Java version. The java version are automatically updated when there is a Linux update.
Fixing your Java version is bad practice.
In folder
/usr/lib/jvm
there are a soft links to current JVM.Probably
/usr/lib/jvm/jre-1.8.0
Suggesting to inspect the Java version installed in your system:
In order to set
JAVA_HOME
to the current Java version.Now change the order to the
PATH
and test again:There is difference in
PATH
. Hope you can appreciate thePATH
mechanism.First check whether your bashrc java path is override by another shell script’s java path in somewhere. Print the $PATH and verify java 11 path is not included in there. If java 11 path also included in $PATH before java 8 it is override in shell script in somewhere.
Check whether
$JAVA_HOME
variable in .bash_profile, /home/.profile, etc/.profile shell scripts. (Or elsegrep
‘java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java’ and find the shell scripts where the $JAVA_HOME configured by java 11 and it found remove those)Then reboot the machine and try
sudo alternatives --config java
again