简体   繁体   中英

Tomcat 8.0.36: WebappClassLoaderBase elusive nullpointerexception

Issue:

The issue is that I am getting a NullPointerException in WebappClassLoaderBase in Tomcat without too much explanation of why that happens. It starts to happen from version 8.0.35 as far as I can tell.....

The exception trace:

java.lang.NullPointerException
    org.apache.catalina.loader.WebappClassLoaderBase.binaryNameToPath(WebappClassLoaderBase.java:2562)
    org.apache.catalina.loader.WebappClassLoaderBase.findLoadedClass0(WebappClassLoaderBase.java:2730)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1208)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

The web.xml which generates the issue:

<?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">

<servlet>
    <servlet-name>FirstServlet</servlet-name>
    <async-supported>true</async-supported>
</servlet>

<servlet>
    <servlet-name>SecondServlet</servlet-name>
    <enabled>false</enabled>
</servlet>

<session-config>
    <session-timeout>7</session-timeout>
    <cookie-config>
        <http-only>true</http-only>
        <secure>true</secure>
    </cookie-config>
</session-config>

<security-constraint>
    <display-name>HTTPS</display-name>
    <web-resource-collection>
        <web-resource-name>HTTPS Constraint</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<!-- IMPORTED web.xml MODULES END -->

</web-app>

I solved it by adding <servlet-class> tags in the web.xml file.

AFAICT I should not need to do this, as in my code I have @WebServlet annotations which should already have registered the class names with Tomcat.

I saw this issue when I developed on Debian Wheezy Linux, Java 8, Jersey 2.24, Tomcat 8.0.38, and tried deploying the war file to a Tomcat 8.0 in my developer files that defaulted to using Java 7 from the operating system. I have multiple versions of Java installed for development. To fix, I created a short script to export the JAVA_HOME and JRE_HOME variables as the corresponding Java 8 locations and then run the Tomcat bin/shutdown.sh and bin/startup.sh in order. This solved the problem.

If the application is being deployed to an operational Tomcat, then the /etc/default/tomcat8 file can be edited to set JAVA_HOME because the default on the OS may be the wrong version.

The bottom line is be careful assuming that Tomcat will find the correct Java version on the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM