JAVA_HOME and PATH are set but java -version still shows the old one

前端 未结 10 1417
温柔的废话
温柔的废话 2020-11-29 01:12

I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOME and $PATH environment variables in ~/.profile as follows:



        
10条回答
  •  情歌与酒
    2020-11-29 02:00

    If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:

    1. I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
    2. I ran:

      sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
      

      Now you can execute sudo update-alternatives --config java and choose your java version.

    3. This doesn't set the JAVA_HOME variable, which I wanted configured, so I just added it to my ~/.bashrc, including an export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35" statement

    Now, I had two JDKs downloaded (let's say the second has been extracted to /home/aqeel/development/jdk/jdk-10.0.1).

    How can we change the JAVA_HOME dynamically based on the current java being used?

    My solution is not very elegant, I'm pretty sure there are better options out there, but anyway:

    1. To change the JAVA_HOME dynamically based on the chosen java alternative, I added this snippet to the ~/.bashrc:

      export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print $2}' | awk -F'/bin/java' '{print $1}')
      

    Finally (this is out of the scope) if you have to change the java version constantly, you might want to consider:

    1. Adding an alias to your ~./bash_aliases:

      alias change-java="sudo update-alternatives --config java"
      

    (You might have to create the file and maybe uncomment the section related to this in ~/.bashrc)

提交回复
热议问题