[Java] UnsupportedClassVersionError was output when the .war file built with Eclipse was placed on EC2 and executed.

4 minute read

environment

Execution environment

AWS EC2 t2.micro Amazon Linux 2 Apache Tomcat/9.0.34

# java -version
openjdk version "1.8.0_252-debug"
OpenJDK Runtime Environment (build 1.8.0_252-debug-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09-debug, mixed mode)

# javac -version
javac 1.8.0_252-debug

Build environment (local environment)

Windows10 Eclipse 2020-03

Java version confirmed by Windows command prompt

> java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

> javac -version
javac 1.8.0_231

Background

To study web application development, create a dynamic web project “example” on Eclipse, Created a sample application “HealthCheck” with Servlet & JSP. Confirm normal operation from the browser with “http://localhost:8080/example/HealthCheck”. It works fine locally.

I wanted to check if it works by deploying on EC2, so every project on Eclipse Export to a .war file and place it on EC2.

[[email protected] webapps]# ls -l /opt/apache-tomcat/webapps
total 24
drwxr-x--- 16 tomcat tomcat 4096 Apr 24 15:28 docs
drwxr-x--- 5 tomcat tomcat 123 Jun 3 12:55 example
drwxr-x--- 6 tomcat tomcat 83 Apr 24 15:28 examples
- rwxr-xr-x 1 tomcat tomcat 18910 Jun 3 12:55 example.war ←★
drwxr-x--- 5 tomcat tomcat 87 Apr 24 15:28 host-manager
drwxr-x--- 5 tomcat tomcat 103 Apr 24 15:28 manager
drwxr-x--- 3 tomcat tomcat 283 Apr 24 15:28 ROOT

[[email protected] webapps]# ls -l /opt/apache-tomcat/webapps/example
total 16
drwxr-x--- 2 tomcat tomcat 31 Jun 3 12:55 css
- rw-r----- 1 tomcat tomcat 761 May 27 00:08 Ex5_2.jsp
- rw-r----- 1 tomcat tomcat 493 May 26 23:31 formSample.jsp
- rw-r----- 1 tomcat tomcat 140 May 25 21:06 hello.html
drwxr-x--- 2 tomcat tomcat 44 Jun 3 12:55 META-INF
- rw-r----- 1 tomcat tomcat 670 May 26 18:38 sample.jsp
drwxr-x--- 5 tomcat tomcat 43 Jun 3 12:55 WEB-INF

Restart tomcat.service and check status

[[email protected] webapps]# systemctl status tomcat.service
● tomcat.service-Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (exited) since Wed 2020-06-03 13:12:27 UTC; 1h 4min ago
  Process: 4100 ExecStop=/opt/apache-tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 4129 ExecStart=/opt/apache-tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 4129 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/tomcat.service
           mq4144 /usr/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat/conf/loggin...

Jun 03 13:12:27 ip-10-0-0-50.ap-northeast-1.compute.internal systemd[1]: Starting Apache Tomca...
Jun 03 13:12:27 ip-10-0-0-50.ap-northeast-1.compute.internal systemd[1]: Started Apache Tomcat...
Hint: Some lines were ellipsized, use -l to show in full.

Event

“500 – Internal Server Error” occurs when connecting from the browser to the EIP attached to EC2 http://[EIP]:8080/example/HealthCheck


HTTP Status 500 – Internal Server Error

Type: Exception Report Message: Error instantiating servlet class [servlet.HealthCheck] Description: The server encountered an unexpected condition. It interferes with the fulfillment of the request. Exception: Javax.servlet.ServletException: Error instantiating servlet class [servlet.HealthCheck] (Omitted) Root cause: Java.lang.UnsupportedClassVersionError: servlet/HealthCheck has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 (unable to load class [servlet .HealthCheck]) (Omitted) Note: All stack traces of the cause are logged in the server

Apache Tomcat/9.0.34

Investigation

Due to Java version difference?

This “UnsupportedClassVersionError” is

While the Java virtual machine is reading a class file, it will Thrown if it is determined that the minor version number is not supported.

something like.

In other words, it is considered that the Java version difference between the build environment and the execution environment is the cause.

As described in “Environment” at the beginning, the version of javac in the build environment is javac 1.8.0_231 The java version of the execution environment is openjdk version "1.8.0_252-debug" Therefore, there seems to be no problem…

Check the version of the compiled class file

[[email protected] webapps]# javap -v ./example/WEB-INF/classes/servlet/HealthCheck.class
Classfile /opt/apache-tomcat-9.0.34/webapps/example/WEB-INF/classes/servlet/HealthCheck.class
  Last modified Jun 4, 2020; size 1988 bytes
  MD5 checksum 83f908e6070439cc6281b73baae5306d
  Compiled from "HealthCheck.java"
public class servlet.HealthCheck extends javax.servlet.http.HttpServlet
  minor version: 0
  major version: 55 ←★
  flags: ACC_PUBLIC, ACC_SUPER

The major version is “55”! !!

On the other hand, the JRE version of the execution environment is 1.8. According to the here site, The class file version supported by JRE version 1.8 is “52”.

I understand the details of the error.

So, the version of Javac confirmed at the “build environment” at the beginning was confirmed for the entire Windows It is the result (confirmed by cmd opened from the start menu), and the project It turns out that the content is different from what you can see in the “command prompt” that appears in the right-click menu.

>javac -version
javac 11.0.5

D:\31_Pleiades\workspace\example>java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)

Looking at this, the version of javac is “11.0.5”. The Eclipse I’m using now is “Pleiades All in One Eclipse”, and there are multiple versions of the JDK in this plugin.

There is a separate JDK 1.8 installed on Windows, and the path goes there, so I did not notice it at the beginning check.

The problem this time occurred because the javac used when building with Eclipse was “11.0.5”. If you can lower the version of javac used when building to “1.8”, this problem will be cleared.

Deal

Try lowering the JDK version at build time

First, check the settings of the whole Eclipse

Try lowering the version as project-specific settings <img src=”https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/613975/5098c56e-e448-264b-360c-4d5087a42cc7.png” width=400)>

Rebuild and check class file version

>javap -v HealthCheck.class
Classfile /D:/31_Pleiades/workspace/example/build/classes/servlet/HealthCheck.class
  Last modified 2020/06/04; size 1988 bytes
  MD5 checksum 5deb7a7fe4b855e66be9d06261116bd6
  Compiled from "HealthCheck.java"
public class servlet.HealthCheck extends javax.servlet.http.HttpServlet
  minor version: 0
  major version: 52 ←★ Success
  flags: ACC_PUBLIC, ACC_SUPER

Export to .war file again → Server allocation → Try to access

… … …

success! !!

So be sure to check the Java version! It was a story. .. ..