skip to Main Content

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


  1. Suggesting to try again with this:

    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/bin
    java -version
    

    The difference in the the 3rd line. Added /bin to path.

    If not working try (thanks to comment from @Jeff Schallerr):

    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/jre/bin
    java -version
    

    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:

    ls -l /usr/lib/jvm
    

    In order to set JAVA_HOME to the current Java version.

    JAVA_HOME=/usr/lib/jvm/jre-1.8.0
    export JAVA_HOME
    export PATH=$JAVA_HOME/bin:$PATH
    which java
    java -version
    $JAVA_HOME/bin/java -version
    

    Now change the order to the PATH and test again:

    JAVA_HOME=/usr/lib/jvm/jre-1.8.0
    export JAVA_HOME
    export PATH=$PATH:$JAVA_HOME/bin
    which java
    java -version
    $JAVA_HOME/bin/java -version
    

    There is difference in PATH. Hope you can appreciate the PATH mechanism.

    Login or Signup to reply.
  2. 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.

    echo $PATH
    

    Check whether $JAVA_HOME variable in .bash_profile, /home/.profile, etc/.profile shell scripts. (Or else grep ‘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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search