How to configure multiple JDK environment variables under Linux(CentOS7)

Keywords: Linux Tomcat JDK Apache vim

I. Linux version

2. Copy and paste multiple jdks, as follows

cp -R jdk1.7.0_80/ jdk1.7.0_80-2

cp -R jdk1.7.0_80/ jdk1.7.0_80-3

3. Configure multiple JDK environment variables

Add the following after the / etc/profile file

# JDK1 environment configuration
export JAVA_HOME=/usr/local/program/jdk1.7.0_80
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

# JDK2 environment configuration
export JAVA_HOME_2=/usr/local/program/jdk1.7.0_80-2
export JRE_HOME_2=$JAVA_HOME_2/jre
export CLASSPATH_2=.:$JAVA_HOME_2/jre/lib/rt.jar:$JAVA_HOME_2/lib/dt.jar:$JAVA_HOME_2/lib/tools.jar
export PATH_2=$JAVA_HOME_2/bin:$PATH

# JDK3 environment configuration
export JAVA_HOME_3=/usr/local/program/jdk1.7.0_80-3
export JRE_HOME_3=$JAVA_HOME_3/jre
export CLASSPATH_3=.:$JAVA_HOME_3/jre/lib/rt.jar:$JAVA_HOME_3/lib/dt.jar:$JAVA_HOME_3/lib/tools.jar
export PATH_3=$JAVA_HOME_3/bin:$PATH

The screenshot is as follows:

Finally, don't forget to make the environment configuration effective

source /etc/profile

Verify that the JDK environment variable configuration is successful by printing the JDK environment variable, as follows

echo $JAVA_HOME
echo $JRE_HOME
echo $CLASSPATH
echo $PATH
echo $JAVA_HOME_2
echo $JRE_HOME_2
echo $CLASSPATH_2
echo $PATH_2
echo $JAVA_HOME_3
echo $JRE_HOME_3
echo $CLASSPATH_3
echo $PATH_3

See whether the printed string is consistent with the content just configured in / etc/profile. If the content is consistent, the configuration is successful

V. for each tomcat, modify the JAVA_HOME, CLASSPATH and other environment variables to be read at startup

1. Modify catalina.sh of apache-tomcat-7.0.90 as follows:

vim /usr/local/program/apache-tomcat-7.0.90/bin/catalina.sh

Specify the JDK environment variable at the beginning of the file, as follows

# JDK1 environment configuration
export JAVA_HOME=/usr/local/program/jdk1.7.0_80
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

Note: it doesn't matter whether the first tomcat JDK is specified or not, because it will read the correct system environment variables by default, namely $JAVA_HOME, $JRE_HOME, $CLASSPATH, $PATH, but the other two copied and pasted tomcat must specify JDK.



2. Modify catalina.sh of apache-tomcat-7.0.90-2 as follows:

vim /usr/local/program/apache-tomcat-7.0.90-2/bin/catalina.sh

Specify the JDK environment variable at the beginning of the file, as follows

# JDK2 environment configuration
export JAVA_HOME_2=/usr/local/program/jdk1.7.0_80-2
export JRE_HOME_2=$JAVA_HOME_2/jre
export CLASSPATH_2=.:$JAVA_HOME_2/jre/lib/rt.jar:$JAVA_HOME_2/lib/dt.jar:$JAVA_HOME_2/lib/tools.jar
export PATH_2=$JAVA_HOME_2/bin:$PATH

3. Modify catalina.sh of apache-tomcat-7.0.90-3 as follows:

vim /usr/local/program/apache-tomcat-7.0.90-3/bin/catalina.sh

Specify the JDK environment variable at the beginning of the file, as follows

# JDK3 environment configuration
export JAVA_HOME_3=/usr/local/program/jdk1.7.0_80-3
export JRE_HOME_3=$JAVA_HOME_3/jre
export CLASSPATH_3=.:$JAVA_HOME_3/jre/lib/rt.jar:$JAVA_HOME_3/lib/dt.jar:$JAVA_HOME_3/lib/tools.jar
export PATH_3=$JAVA_HOME_3/bin:$PATH

Vi. start three Tomcat to see if the JDK read by Tomcat is the one we just configured

The command and screenshot are as follows:

/usr/local/program/apache-tomcat-7.0.90/bin/startup.sh
/usr/local/program/apache-tomcat-7.0.90-2/bin/startup.sh
/usr/local/program/apache-tomcat-7.0.90-3/bin/startup.sh

Or take a look at the Java process

ps -ef |grep java

Multiple jdks have been configured

Posted by lasse48 on Fri, 06 Dec 2019 02:21:17 -0800