Issue
I'm creating a api rest with Tomcat 7.0 + Maven + Java EE + JDK 10.0.2 in eclipse and I got this error:
Error:
HTTP Status 500 - Servlet.init() for servlet api threw exception
type Exception report
message Servlet.init() for servlet api threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception javax.servlet.ServletException: Servlet.init() for servlet api threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) java.base/java.lang.Thread.run(Thread.java:844)
root cause java.lang.IllegalArgumentException jersey.repackaged.org.objectweb.asm.ClassReader.(ClassReader.java:170) jersey.repackaged.org.objectweb.asm.ClassReader.(ClassReader.java:153) jersey.repackaged.org.objectweb.asm.ClassReader.(ClassReader.java:424) com.sun.jersey.spi.scanning.AnnotationScannerListener.onProcess(AnnotationScannerListener.java:138) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner$2.f(WebAppResourcesScanner.java:104) com.sun.jersey.core.util.Closing.f(Closing.java:71) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:102) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:89) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:89) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:89) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:89) com.sun.jersey.spi.scanning.servlet.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:79) com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:80) com.sun.jersey.api.core.servlet.WebAppResourceConfig.init(WebAppResourceConfig.java:102) com.sun.jersey.api.core.servlet.WebAppResourceConfig.(WebAppResourceConfig.java:89) com.sun.jersey.api.core.servlet.WebAppResourceConfig.(WebAppResourceConfig.java:74) com.sun.jersey.spi.container.servlet.WebComponent.getWebAppResourceConfig(WebComponent.java:668) com.sun.jersey.spi.container.servlet.ServletContainer.getDefaultResourceConfig(ServletContainer.java:435) com.sun.jersey.spi.container.servlet.ServletContainer.getDefaultResourceConfig(ServletContainer.java:602) com.sun.jersey.spi.container.servlet.WebServletConfig.getDefaultResourceConfig(WebServletConfig.java:87) com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:699) com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:674) com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:205) com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394) com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577) javax.servlet.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) java.base/java.lang.Thread.run(Thread.java:844)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.
Apache Tomcat/7.0.47
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>api</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>api</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
</project>
Solution
After a bit of digging, I figured that the IllegalArgumentException
is being thrown by Jersey's repackaged ASM library.
I can't be sure I have the right version, but at line 170, the code is like this:
// checks the class version
// added "support" for java 9 compiled classes
if (readShort(off + 6) > Opcodes.V9) {
throw new IllegalArgumentException();
}
The code is checking the classfile version number for the class it is attempting to read, and throwing IllegalArgumentException
if it is too recent.
In short, you are trying to use some Java class that was compiled with a Java compiler that is too new for the ASM library:
- Use an older JDK or set the
-target
version to be older. - Alternatively, find / install a version of Jersey that is certified for the Java version you are building for.
One thing that strikes me as odd is that you have an explicit dependency on an ancient version "asm" in your POM file. However, the stacktrace indicates that you are not using that version.
Answered By - Stephen C