Install Java On Ubuntu By CommandLine

Installing JRE/JDK

1
2
3
4
sudo apt-get update  # update the package index
java -version # check if java installed
sudo apt-get install default-jre # install JRE
sudo apt-get install default-jdk # install JDK, if necessary

Setting the “JAVA_HOME” environment variable

  1. find out the path of JAVA installation

    1
    sudo update-alternatives --config java

    It perhaps returns:

    1
    There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

    Then, the path is “/usr/lib/jvm/java-7-openjdk-amd64”, hold it.

  2. use nano to edit the file /etc/environment

    1
    sudo nano /etc/environment

    In this file, append the path, holden just now, into the quotes, seperate by colon, like this:

    1
    JAVA_HOME="your/other/paths:/usr/lib/jvm/java-7-openjdk-amd64"
  3. reload the file /etc/environment

    1
    source /etc/environment
  4. check if java installed

    1
    java -version

Finally, it maybe not have to set the environment variable. But I don’t test that.

Reference