Search code examples
javalinux-mint-19

Javac is not same version as java


Linux Mint 19.3

In file etc/profile:

export PATH=$PATH:$JAVA_HOME/bin

run set in terminal:

JAVA_HOME=/usr/java/jdk1.8.0_251
PATH=/usr/java/jdk1.8.0_251/bin

In terminal run

java -version

java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

It's correct.

But when I run

javac -version I get another version.

javac 11.0.7

I need javac to be version 1.8 (same as java)


Solution

  • Root cause of the problem is due to JAVA_HOME being added at the end of the PATH.


    The problem can be fixed by adding JAVA_HOME to beginning of PATH, as follows:

    export PATH=$JAVA_HOME/bin:$PATH
    

    Working example:

    Suppose that JDK 11 is installed in /opt/jdk-11 directory. Here are the steps to use java version 11 for both java and javac:

    1. Update JAVA_HOME and PATH

    export JAVA_HOME=/opt/jdk-11
    export PATH=$JAVA_HOME/bin:$PATH
    

    2. Verify that both java and javac are picked from /opt/jdk-11/bin

    $ which java
    /opt/jdk-11/bin/java
    
    $ which javac
    /opt/jdk-11/bin/javac
    

    3. Verify the output of java and javac

    $ java -version
    java version "11.0.7" 2020-04-14 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)
    
    $ javac -version
    javac 11.0.7