How to set JAVA path to JDK 8

8,902

You can use "alternatives" to switch between different java versions:

sudo update-alternatives --config java

And also for the java compiler (javac) if you are compiling some java code:

sudo update-alternatives --config javac

If you installed Java 8 from apt openjdk:

sudo apt install openjdk-8-jdk

Then your java home must be: /usr/lib/jvm/java-8-openjdk-amd64

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

If java 8 is not visible in update-alternatives you can install it with:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1000

Then check it's there with:

update-alternatives --list java
Share:
8,902

Related videos on Youtube

Rishabh
Author by

Rishabh

Updated on September 18, 2022

Comments

  • Rishabh
    Rishabh almost 2 years

    Was trying to contribute to some open-source project. And I tried finding the answer online, tried Wikipedia how to set it. None works. This is getting irritating. I need to commit my work. As I run git commit here is what I get:

    please make sure JAVA_HOME is set to JDK8
    

    Now, I use JAVA 11. I uninstalled JAVA 11 then tried installing JAVA 8. No success, tried installing 12 again JDK8 is needed.

    echo $JAVA_HOME
    

    gives

    /usr/lib/jvm/java-12-oracle
    
    java -version
    

    is 12.0.2

    I want to install Java 8 and Java 12 both on my computer. And frequently shift between two. I want to be able to work on projects which work with java 8 and those which work with java 12.

    dpkg -L openjdk-8-jdk
    

    Gives

    /.
    /usr
    /usr/lib
    /usr/lib/jvm
    /usr/lib/jvm/java-8-openjdk-amd64
    /usr/lib/jvm/java-8-openjdk-amd64/bin
    /usr/lib/jvm/java-8-openjdk-amd64/bin/appletviewer
    /usr/lib/jvm/java-8-openjdk-amd64/bin/jconsole
    /usr/lib/jvm/java-8-openjdk-amd64/include
    /usr/lib/jvm/java-8-openjdk-amd64/include/jawt.h
    /usr/lib/jvm/java-8-openjdk-amd64/include/linux
    /usr/lib/jvm/java-8-openjdk-amd64/include/linux/jawt_md.h
    /usr/lib/jvm/java-8-openjdk-amd64/lib
    /usr/lib/jvm/java-8-openjdk-amd64/lib/jconsole.jar
    /usr/lib/jvm/java-8-openjdk-amd64/man
    /usr/lib/jvm/java-8-openjdk-amd64/man/ja_JP.UTF-8
    /usr/lib/jvm/java-8-openjdk-amd64/man/ja_JP.UTF-8/man1
    /usr/lib/jvm/java-8-openjdk-amd64/man/ja_JP.UTF-8/man1/appletviewer.1.gz
    /usr/lib/jvm/java-8-openjdk-amd64/man/ja_JP.UTF-8/man1/jconsole.1.gz
    /usr/lib/jvm/java-8-openjdk-amd64/man/man1
    /usr/lib/jvm/java-8-openjdk-amd64/man/man1/appletviewer.1.gz
    /usr/lib/jvm/java-8-openjdk-amd64/man/man1/jconsole.1.gz
    /usr/share
    /usr/share/doc
    /usr/share/doc/openjdk-8-jre-headless
    /usr/share/doc/openjdk-8-jre-headless/test-amd64
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/check-hotspot-hotspot.log.gz
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/check-jdk-hotspot.log.gz
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/check-langtools-hotspot.log.gz
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/failed_tests-hotspot.tar.gz
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/jtreg-summary-hotspot.log.gz
    /usr/share/doc/openjdk-8-jre-headless/test-amd64/jtreport-hotspot.tar.gz
    /usr/share/doc/openjdk-8-jdk
    
  • Rishabh
    Rishabh almost 5 years
    /usr/lib/jvm/java-8-openjdk-amd64 : There is no such directory it is showing.
  • Radhwane Chebaane
    Radhwane Chebaane almost 5 years
    yes, JAVA_HOME is a directory not a file. It points to the root directory contianing Java install files.
  • Rishabh
    Rishabh almost 5 years
    sudo update-alternatives --config java shows me only java12. But when I do sudo apt install openjdk-8-jdk. It says openjdk-8-jdk is already the newest version, that means jdk 8 is already installed
  • Radhwane Chebaane
    Radhwane Chebaane almost 5 years
    Then you probably need to tell update-alternatives where is installed the java 8: update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1000
  • Rishabh
    Rishabh almost 5 years
    it is showing me error: alternative path /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java doesn't exist
  • Rishabh
    Rishabh almost 5 years
    I ran : update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1000
  • Rishabh
    Rishabh almost 5 years
    Further $JAVA_HOME gives me : bash: /usr/lib/jvm/jdk1.8.0: No such file or directory
  • Radhwane Chebaane
    Radhwane Chebaane almost 5 years
    If you successfully installed openjdk-8-jdk, could you give me the output of this command ? dpkg -L openjdk-8-jdk
  • Rishabh
    Rishabh almost 5 years
    I have edited the question.
  • Radhwane Chebaane
    Radhwane Chebaane almost 5 years
    Ok, try to set java8 again into alternatives: update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1000 Then check it's there with: update-alternatives --config java
  • Rishabh
    Rishabh almost 5 years
    Again, it is showing me update-alternatives: error: alternative path /usr/lib/jvm/java-8-openjdk-amd64/bin/java doesn't exist
  • Radhwane Chebaane
    Radhwane Chebaane almost 5 years
    If file /usr/lib/jvm/java-8-openjdk-amd64/bin/java does not exist, then you must reinstall java again: apt purge java* openjdk* apt install openjdk-8-jdk If you still need java 11 install openjdk-11-jdk
  • Rishabh
    Rishabh almost 5 years
    This is causing me headaches. sudo apt purge java* openjdk* don't work this causes. sudo apt install openjdk-8-jdk to show me no new installation.