Causes and remedies for the exception "ServletException: Error instantiating servlet class" when deploying Java applications

Situation when an exception occurs

When I created a Servlet app in Eclipse and deployed it on a VPS, the following exception occurred.

javax.servlet.ServletException: Error instantiating servlet class [servlet.LoginServlet]

error.png

Cause

The JDK version of Eclipse and the JRE version of Tomcat did not match, causing Tomcat to be unable to execute class files. (If the Java version at compile time is higher than the Java version at runtime, an exception seems to occur.)


Error log

Root cause

java.lang.UnsupportedClassVersionError: 
servlet/LoginServlet 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.LoginServlet])
 
~ Abbreviated below ~
JRE version Class file version
Java 8 52.0
Java 9 53.0
Java 10 54.0
Java 11 55.0
Java 12 56.0
Java 13 57.0
Java 14 58.0
Java 15 59.0

In this case, the app was compiled with version 55.0 (Java 11), but was trying to run with version 52.0 (Java 8).

solution

Update the version on the Tomcat side or lower the version on the Eclipse side. (I decided to change the Eclipse side this time.)   The setting screen is displayed when the transition from the Eclipse window to Window> Settings` ``. When you move to the Java> Compiler`` page, there is an item called ``Compiler compliance level`` of the JDK, so change this to ``1.8```. eclipse設定.png

reference

https://stackoverflow.com/questions/47457105/class-has-been-compiled-by-a-more-recent-version-of-the-java-environment http://www.ne.jp/asahi/hishidama/home/tech/java/version.html https://jpn.itlibra.com/article?id=21118

Recommended Posts

Causes and remedies for the exception "ServletException: Error instantiating servlet class" when deploying Java applications
When deploying to Heroku, OpenApp causes an error
[Ruby On Rails] Causes and remedies for ActionView :: Template :: Error (ActiveStorage :: InvariableError) (hypothesis / verification)
Error logging and exception handling that you can't often see in the Java area
[Beginner] Commands and knowledge that may be useful for error resolution when deploying AWS
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class