首页 > 解决方案 > @WebServlet 在 GlassFish 5 + JDK 13 中失败并出现 java.lang.RuntimeException: com.example.NewServlet。(NewServlet.java:1),总是在第 1 行

问题描述

JSP 和 html 文件由 NetBeans 中的 Glassfish 服务器提供,但不是 servlet。最新的 NetBeans 配置为使用 JDK 最新 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 类的第 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$9.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.access$100(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)

Netbeans 生成的未改动的 servlet shell:

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>
}

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

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!|#]

标签: servletsnetbeansglassfishjava-13

解决方案


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

但是,这意味着当您在 NetBeans 中启动 Glassfish 5 服务器时,它也将使用 Java 13。请参阅 NetBeans 错误报告NETBEANS-2256 NetBeans 11 vc3: Unable to manage GlassFish 5.1 when running NetBeans using any JDK 8 以上说明:

如果我使用 JDK 9+ 运行 Apache NetBeans,则无法从 IDE 中管理 GlassFish 服务器或部署到 GlassFish 服务器。 GlassFish 5+ 目前仅支持 JDK 8 ...

此问题不仅限于 NetBeans IDE。例如,请参阅Eclipse Glassfish/Payara 无法识别我的 JDK

您的选择是:

  • 编辑netbeans.conf以使 JDK 8 成为 NetBeans 11.x 的默认 Java 版本。有关如何执行此操作的详细信息,请参阅此 SO 答案。请注意,这不会阻止您在 NetBeans 中为您的项目添加 JDK 13 作为第二个 Java 平台(使用Tools > Java Platforms > Add Platform...)。如果您这样做,请确保您的 JDK 8 更新版本与您的 Glassfish 5.x 版本兼容,如这个有用的 SO 答案中所述。
  • 使用 JDK 13 时,请在 NetBeans 11.2 中使用替代应用程序服务器。例如Wildfly 18+ 支持使用 JDK 13

推荐阅读