@WebServlet 在 GlassFish 5 + JDK 13 和 java.lang.RuntimeException 中失败:com.example.NewServlet.<init>(NewServlet.java:1),总是在第 1 行

@WebServlet fails in GlassFish 5 + JDK 13 with java.lang.RuntimeException: com.example.NewServlet.<init>(NewServlet.java:1), always at line 1

JSP 和 html 文件由我在 NetBeans 中的 Glassfish 服务器提供,但不是 servlet。最新的 NetBeans 配置为使用 JDK latest 13,Glassfish 使用 Java EE 1.8

浏览器报错:

    HTTP Status 500 - Internal Server Error    
    
    type Exception report
    message
Internal Server Error
    description
The server encountered an internal error that prevented it from fulfilling this request.
    exception
    javax.servlet.ServletException: Error instantiating servlet class com.example.NewServlet
    root cause
    com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class com.example.NewServlet
    root cause
    java.lang.RuntimeException:
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 5.1.0 logs.

服务器上报告错误(请注意它总是在 sevlet class 的第 1 行!这是通常声明 package 的地方):

 StandardWrapperValve[NewServlet]: Allocate exception for servlet NewServlet
java.lang.RuntimeException:
 at com.example.NewServlet.<init>(NewServlet.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.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:119)
 at org.jboss.weld.injection.ConstructorInjectionPoint.invokeAroundConstructCallbacks(ConstructorInjectionPoint.java:92)
 at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:78)
 at org.jboss.weld.injection.producer.AbstractInstantiator.newInstance(AbstractInstantiator.java:28)
 at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:112)
 at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:180)
 at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:435)
 at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:293)
 at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:453)
 at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:406)
 at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:312)
 at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:725)
 at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1955)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1262)
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1069)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
 at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
 at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:75)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
 at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
 at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:439)
 at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:144)
 at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
 at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
 at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
 at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:95)
 at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
 at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
 at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
 at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
 at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
 at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:515)
 at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
 at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
 at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:33)
 at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
 at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
 at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
 at java.lang.Thread.run(Thread.java:748)

未修改的 servlet shell 由 Netbeans 生成:

public class NewServlet extends HttpServlet {
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try ( PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

查看模糊相似的解决方案post,还尝试添加无参数构造函数:

public NewServlet() {
        super();
}

Glassfish 初创公司的恶作剧:

Launching GlassFish on Felix platform
INFO: Create bundle provisioner class = class com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner.
WARNING: Skipping entry  because it is not an absolute URI.
WARNING: Skipping entry  because it is not an absolute URI.
Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishRuntime@3b4af5 in service registry.
#!## LogManagerService.postConstruct : rootFolder=D:\GlassfishDownload\glassfish
#!## LogManagerService.postConstruct : templateDir=D:\GlassfishDownload\glassfish\lib\templates
#!## LogManagerService.postConstruct : src=D:\GlassfishDownload\glassfish\lib\templates\logging.properties
#!## LogManagerService.postConstruct : dest=D:\GlassfishDownload\glassfish\domains\domain1\config\logging.properties
  Running GlassFish Version: GlassFish Server Open Source Edition  5.1.0  (build default-private)|#]
  Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter|#]
  Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.|#]
  Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.|#]
  Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.|#]
  Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry|#]
  Authorization Service has successfully initialized.|#]
  JTS5014: Recoverable JTS instance, serverId = [100]|#]
  Grizzly Framework 2.4.4 started in: 81ms - bound to [/0.0.0.0:8080]|#]
  Grizzly Framework 2.4.4 started in: 5ms - bound to [/0.0.0.0:8181]|#]
  Grizzly Framework 2.4.4 started in: 5ms - bound to [/0.0.0.0:4848]|#]
  Grizzly Framework 2.4.4 started in: 5ms - bound to [/0.0.0.0:3700]|#]
  visiting unvisited references|#]
  HV000001: Hibernate Validator 6.0.10.Final|#]
  Grizzly Framework 2.4.4 started in: 6ms - bound to [/0.0.0.0:8080]|#]
  Grizzly Framework 2.4.4 started in: 7ms - bound to [/0.0.0.0:8181]|#]
  Java security manager is disabled.|#]
  Entering Security Startup Service.|#]
  Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.|#]
  Security Service(s) started successfully.|#]
  Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080|#]
  Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181|#]
  Created HTTP listener admin-listener on host/port 0.0.0.0:4848|#]
  Created virtual server server|#]
  Created virtual server __asadmin|#]
  Setting JAAS app name glassfish-web|#]
  Virtual server server loaded default web module |#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  WELD-000900: 3.0.0 (Final)|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] public org.hibernate.validator.cdi.ValidationExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  Listening to REST requests at context: /management/domain.|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.ibm.jbatch.container.cdi.BatchProducerBean is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.WebAppExceptionHolder is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.WebAppExceptionInterceptor is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionMapper is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider$JaxRsParamProducer is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorMandatory is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorNever is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorNotSupported is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorRequired is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorRequiresNew is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorSupports is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.servlet.internal.CdiExternalRequestScope is deprecated from CDI 1.1!|#]
  Initializing Soteria 1.0.1 for context '/BackToTech'|#]
  Initializing Mojarra 2.3.9 for context '/BackToTech'|#]
  Loading application [BackToTech] at [/BackToTech]|#]
  Loading application BackToTech done in 17,036 ms|#]
  GlassFish Server Open Source Edition  5.1.0  (default-private) startup time : Felix (9,720ms), startup services(18,529ms), total(28,249ms)|#]
  Grizzly Framework 2.4.4 started in: 21ms - bound to [/0.0.0.0:7676]|#]
  visiting unvisited references|#]
  Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@b224cd as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@1ee25f1.|#]
  JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://DESKTOP-2IGRR7A:8686/jndi/rmi://DESKTOP-2IGRR7A:8686/jmxrmi|#]
  Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181|#]
  Grizzly Framework 2.4.4 started in: 7ms - bound to [/0.0.0.0:8181]|#]
  Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080|#]
  Grizzly Framework 2.4.4 started in: 13ms - bound to [/0.0.0.0:8080]|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  visiting unvisited references|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] public org.hibernate.validator.cdi.ValidationExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorMandatory is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorNever is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorNotSupported is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorRequired is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorRequiresNew is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.cdi.transaction.TransactionalInterceptorSupports is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.ibm.jbatch.container.cdi.BatchProducerBean is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.WebAppExceptionHolder is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.WebAppExceptionInterceptor is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionMapper is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider$JaxRsParamProducer is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!|#]
  WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class org.glassfish.jersey.ext.cdi1x.servlet.internal.CdiExternalRequestScope is deprecated from CDI 1.1!|#]

假设“Latest NetBeans”指的是NetBeans 11.2,Glassfish问题是由于运行 NetBeans使用JDK 13引起的。没有什么直接错误用这种方法;从 NetBeans 11.2 开始明确支持它。

但是,这意味着当您在 NetBeans 中启动 Glassfish 5 服务器时,它也将使用 Java13。请参阅 NetBeans 错误报告 NETBEANS-2256 NetBeans 11 vc3:当 运行 NetBeans 使用大于 JDK 8 的任何 JDK 时,无法管理 GlassFish 5.1,其中指出:

If I run Apache NetBeans using JDK 9+, then I am unable to manage GlassFish server or deployments to GlassFish server from within the IDE. GlassFish 5+ currently only supports JDK 8...

此问题不仅限于 NetBeans IDE。例如,参见

您的选择是:

  • 编辑 netbeans.conf 使 JDK 8 成为 NetBeans 11.x 的默认 Java 版本。 on how to do that. Note that this doesn't prevent you adding JDK 13 as a second Java platform within NetBeans (using Tools > Java Platforms > Add Platform...) for your projects. If you do this, ensure that your update version of JDK 8 is compatible with your version of Glassfish 5.x, as specified in .
  • 使用 JDK 13 时,在 NetBeans 11.2 中使用备用应用程序服务器。例如 Wildfly 18+ supports the use of JDK 13.

Netbeans 12 在 Server COnfiguration 中有一个 属性 配置,我试图用 jdk 11 配置 Glassfish 5.1 运行(jdk 13 是不允许的)但是它无论如何都行不通