【Java】安装

Posted by 西维蜀黍 on 2020-07-25, Last Modified on 2021-09-21

Ubuntu

Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

$ apt install default-jre

The JRE will allow you to run almost all Java software.

Verify the installation with:

$ java -version

You’ll see the following output:

Outputopenjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:

$ sudo apt install default-jdk

Verify that the JDK is installed by checking the version of javac, the Java compiler:

$ javac -version

You’ll see the following output:

Outputjavac 11.0.7

Setting the JAVA_HOME Environment Variable

Many programs written using Java use the JAVA_HOME environment variable to determine the Java installation location.

To set this environment variable, first determine where Java is installed. Use the update-alternatives command:

sudo update-alternatives --config java

This command shows each installation of Java along with its installation path:

OutputThere are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
* 2            /usr/lib/jvm/java-11-oracle/bin/java             1091      manual mode

Press <enter> to keep the current choice[*], or type selection number:

In this case the installation paths are as follows:

  1. OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
  2. Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java.

Copy the path from your preferred installation. Then open /etc/environment using nano or your favorite text editor:

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the /bin portion of the path:

/etc/environment

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/java"

Modifying this file will set the JAVA_HOME path for all users on your system.

Save the file and exit the editor.

Now reload this file to apply the changes to your current session:

source /etc/environment

Verify that the environment variable is set:

echo $JAVA_HOME

You’ll see the path you just set:

Output/usr/lib/jvm/java-11-openjdk-amd64

Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.

Reference