Tomcat has many JAVA environments, JVM version viewing and use priority

I. Introduction

  The business system includes a PC terminal and a mobile terminal, and the mobile terminal is a WeChat applet. The message sent by the applet client did not receive a response, and the system background log was checked to find an error log. The "local_policy.jar" and "US_export_policy.jar" that come with JDK or JRE are encryption algorithms that support 128-bit keys, but when we want to use 256-bit key algorithms, it is beyond its scope and cannot be supported. That's why the exception of "java.security.InvalidKeyException: Illegal key size or default parameters" is reported. According to Baidu query processing, the blogger replaced the two related jar packages in jdk, restarted the system, and found that this error was still reported. The blogger further checked and found that the original deployment method of the blogger was tomcat multi-instance deployment, and the jar package under the instance was replaced, but the jdk8 version was also installed at the operating system level, and the jre environment variable was configured, and tomcat was used first when running The java version in the jre environment variable. If the JRE_HOME environment variable cannot be found, use the java version in the JAVA_HOME environment variable. That's the reason for this blog post.

2. Solve the error of WeChat applet encryption and decryption

1. The backend of the WeChat applet reports an error

报错信息:Forwarding to error page from request [/wx/*******/callback/] due to exception [java.security.InvalidKeyException: Illegal key size]
insert image description here

2. Check the JAVA environment variable

$ echo $JRE_HOME
$ echo $JAVA_HOME
insert image description here

3. Check the java version

$ java -version

4. Download environment variables that support 256-bit encryption and decryption algorithms

  You can download two jar packages (local_policy.jar and US_export_policy.jar) that support 256-bit encryption and decryption algorithms from the official website or CSDN, CSDN download link: java supports 256-bit encryption algorithms required jar packages .

5. The downloaded jar package will replace the existing jar package

  Upload the downloaded jar package to the server, and determine the directory to be replaced according to the java environment variable checked in the second step. If there is a JRE_HOME environment variable, replace the jar package under the JRE_HOME environment variable path first, and if not, replace the JAVA_HOME environment The jar package under the variable path.

insert image description here

6. Restart the application

  Restart the system service after the replacement, send the message again, check the system background log and no error will be reported, and the client can also receive the message reply normally!

$ ./bin/shutdown.sh
$ ./bin/startup.sh

3. Tomcat operation depends on java environment variable priority experiment

  The actual operating environment of the blogger is quite special. In order to verify the priority order of using tomcat corresponding to the java environment, the blogger prepared two java versions and configured and enabled the manager service. Through the manager service, you can view the java version of the JVM environment that the tomcat instance depends on.

1. Upload two java versions to the server

[root@s142 local]# ll |grep -E “java|jdk”
lrwxrwxrwx. 1 root root 12 Apr 6 15:34 java -> jdk1.8.0_291
lrwxrwxrwx. 1 root root 12 Apr 6 15:34 java241 -> jdk1.8.0_241
drwxr-xr-x. 7 root root 245 Dec 11 2019 jdk1.8.0_241
drwxr-xr-x. 8 root root 273 Apr 8 2021 jdk1.8.0_291
-rw-r–r–. 1 root root 194545143 Mar 13 2020 jdk-8u241-linux-x64.tar.gz
-rw-r–r–. 1 root root 144935989 Jun 16 2021 jdk-8u291-linux-x64.tar.gz

2. Configure java environment variables

  The operating system installs two java versions, one is used to specify the JAVA_HOME environment variable, and the other is used to configure the JRE_HOME environment variable configuration. We check the current java version in the operating system is the version configured by the JAVA_HOME environment variable.

[root@s142 local]# cat /etc/profile

#java env test
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java241/jre
export PATH=${JAVA_HOME}/bin:$PATH

(base) [wuhs@s142 tomcat8]$ java -version
java version “1.8.0_291”
Java™ SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot™ 64-Bit Server VM (build 25.291-b10, mixed mode)

3. Deploy a tomcat instance

  Deploy a tomcat instance, how to deploy the manager, see the blog post Tomcat service management page manager deployment .

4. Start the service

(base) [wuhs@s142 tomcat8]$ java -version
java version “1.8.0_291”
Java™ SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot™ 64-Bit Server VM (build 25.291-b10, mixed mode)
(base) [wuhs@s142 tomcat8]$ ./bin/startup.sh

5. Check the JVM version

  Start the tomcat instance, we can see that the tomcat instance is running on the java version of the JRE_HOME environment variable.
insert image description here

6. Modify environment variables

  We modify the environment variable configuration and cancel the JRE_HOME environment variable configuration.
[root@s142 local]# cat /etc/profile
 …

#java env test
export JAVA_HOME=/usr/local/java
#export JRE_HOME=/usr/local/java241/jre
export PATH=${JAVA_HOME}/bin:$PATH
[root@s142 local]# java -version
java version “1.8.0_291”
Java™ SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot™ 64-Bit Server VM (build 25.291-b10, mixed mode)

7. Restart the service

(base) [wuhs@s142 tomcat8]$ ./bin/shutdown.sh
(base) [wuhs@s142 tomcat8]$ ./bin/startup.sh

8. Check the JVM version again

  At this point, you can see that the JVM version is the version specified by the JAVA_HOME configuration.
insert image description here

9. Start log view

  In fact, in the single instance operation, we can see that the startup log has displayed the environment variable settings. No matter how to configure tomcat, the JRE_HOME configuration is used internally, but when the operating system JRE_HOME configuration cannot be found, JRE_HOME The configuration will take the value of JAVA_HOME and assign it to JRE_HOME.

(base) [wuhs@s142 tomcat8]$ ./bin/startup.sh
Using CATALINA_BASE: /home/wuhs/tomcat8
Using CATALINA_HOME: /home/wuhs/tomcat8
Using CATALINA_TMPDIR: /home/wuhs/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /home/wuhs/tomcat8/bin/bootstrap.jar:/home/wuhs/tomcat8/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

10. Summary

  In fact, the java environment that the Tomcat instance depends on when running is based on JRE_HOME. If it cannot be found, use JAVA_HOME to assign the value to JRE_HOME. If it is running in a multi-instance environment, in order to reduce the impact of possible multiple java versions of the system, it is recommended to use JRE_HOME to define the java environment.

Guess you like

Origin blog.csdn.net/carefree2005/article/details/130013047