Home >php教程 >PHP开发 >Configuring Java environment variables under Linux

Configuring Java environment variables under Linux

高洛峰
高洛峰Original
2016-12-17 13:35:131874browse

Generally speaking, we will install Java in the /usr/local directory or /opt directory. It is assumed here that the java installation package has been decompressed under /opt, and the specific directory is: /opt/java8/java1.8.0_45 directory (note: if it is a production environment, the root user must be installed and configured).

Let’s configure the java environment variables. Open /etc/profile and add:

export JAVA_HOME=/opt/jdk1.8.0_45
export JRE_HOME=$JAVA_HOME/jre                 #tomcat需要
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

Note: Be sure not to include spaces in the environment variables, otherwise an error will be reported.

After updating the profile, we execute the following statement (read and execute):

$ source  /etc/profile

Finally execute:

$ java -version

See whether the Java installation and configuration is successful! ! For the modified configuration file to finally take effect, you still need to log out and log in again or restart the computer! !

Because many Linux systems have openJDK installed by default, we have to do another job to set our newly installed Java as the default.

1. First check what java is available in the current environment

$ sudo update-alternatives --display java

Configuring Java environment variables under Linux

The current explicit default on my machine is openjdk7, and I switched to my own newly installed oracle java8.

2. Use update-alternatives to configure the newly installed oracle java8

$ sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_45/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_45/bin/javac 1
$ sudo update-alternatives --set java /opt/jdk1.8.0_45/bin/java
$ sudo update-alternatives --set javac /opt/jdk1.8.0_45/bin/javac

The above command is to configure the path of the new jdk and set it as the default! The update-alternatives --set command is equivalent to the update-alternatives --config command. If you do not use update-alternatives --set but use update-alternatives --config, like this:

Configuring Java environment variables under Linux



For more articles related to configuring Java environment variables under Linux, please pay attention to PHP Chinese website !

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn