使用 Netbeans 和 Tomcat 的任何 ServletContextListener 的第 1 行出现 RuntimeException

RuntimeException at line1 of any ServletContextListener using Netbeans and Tomcat

我将 Netbeans 12.3 用作 IDE,我曾管理由 tomcat 提供服务的不同 Web 应用程序项目。

一天又一天,我无法再运行任何网络项目了。

我什至用一个新的空项目和一个空的 ServletContextListener 实现进行了测试

package stica;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class NewServletListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {}

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}

我的所有 Web 应用程序的结果都相同:

30-Jun-2021 21:37:00.981 GRAVE [http-nio-8080-exec-28] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class [stica.NewServletListener]
    java.lang.RuntimeException: 
        at stica.NewServletListener.<init>(NewServletListener.java:1)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4640)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5172)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:692)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:689)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:537)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1683)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:288)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1592)
        at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:1021)
        at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:356)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:126)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:667)
        at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:378)
        at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:56)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
30-Jun-2021 21:37:00.981 GRAVE [http-nio-8080-exec-28] org.apache.catalina.core.StandardContext.listenerStart Skipped installing application listeners due to previous error(s)

使用 web.xml:

加载侦听器
<listener>
    <description>ServletContextListener</description>
    <listener-class>stica.NewServletListener</listener-class>
</listener>

第 1 行的 RuntimeException 没有其他有意义的(至少对我来说不是)解释。

我尝试了不同版本的 Tomcat(8.5.50、9.0.44、9.0.46)和不同的 jdk 组合用于源、库等(8 和 14,基础IDE)。 我什至尝试了两个 netbeans 版本:12 和 12.3。

无论我做什么,项目都不 运行。

我很绝望

好吧,我找到了原因...很愚蠢。我 post 希望它能对其他可能落入这个陷阱的人有所帮助。

我的 Netbeans 默认使用 java 14 jdk(我错误地认为它是内置的 - 这就是为什么我之前没有按照@skomisa 的建议进行测试)

为了使用它,你必须安装 nb-javac 插件,它通常总是存在的,因为 netbeans 本身会提示你在第一个添加它 运行 全新安装。 不知道为什么在我身上不是...