将 jetty 7 更新为 jetty 8:java.lang.NoClassDefFoundError:javax/servlet/FilterRegistration

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7785021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:26:42  来源:igfitidea点击:

Updating jetty 7 to jetty 8 : java.lang.NoClassDefFoundError: javax/servlet/FilterRegistration

javajetty

提问by sockeqwe

im trying to develop an web server by embedding jetty. So with jetty 7.3 everything worked fine. Yesterday I updated the jetty libaries to the newest version 8.0.3 and now I get an Exception by creating a ServletContextHandler.

我试图通过嵌入码头来开发一个网络服务器。所以使用 jetty 7.3 一切正常。昨天我将码头库更新到最新版本 8.0.3,现在我通过创建一个 ServletContextHandler 得到一个异常。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/FilterRegistration at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:126) at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:106) at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:94) at org.gemsjax.server.GemsJaxServer.main(GemsJaxServer.java:38)

线程“main”中的异常 java.lang.NoClassDefFoundError: javax/servlet/FilterRegistration at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:126) at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java) :106) 在 org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:94) 在 org.gemsjax.server.GemsJaxServer.main(GemsJaxServer.java:38)

So what I do is:

所以我要做的是:

    public static void main(String[] args) {

     Server server = new Server(8080);


        ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContext.setContextPath("/servlets");
        servletContext.addServlet(new ServletHolder( new CollaborationWebSocketServlet()),"/collaboration");


        // The ResourceHandler to handle static web content
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(true);
        resourceHandler.setWelcomeFiles(new String[]{ "index.html" });


        resourceHandler.setResourceBase("./war/");


        ContextHandler resourceContext = new ContextHandler();
        resourceContext.setContextPath("/static");
        resourceContext.setHandler(resourceHandler);



        HandlerCollection handlers = new HandlerCollection();


        handlers.addHandler(resourceContext);
        handlers.addHandler(servletContext);

        server.setHandler(handlers);

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }


}

So the line that throw the exception is:

所以抛出异常的行是:

ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);

Im using ubuntu 11.04 with:

我在 ubuntu 11.04 上使用:

openjdk java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

openjdk java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64-Bit Server VM (build 20.0-b11,混合模式)

Does anyone have a suggestion?

有人有建议吗?

回答by BalusC

The javax.servlet.FilterRegistrationclasswas introduced in Servlet 3.0. This exception suggests that you have still libraries of an older Servlet API version in your runtime classpath which got precedence in classloading. For example a randomly from the Internet picked servlet-api.jarfile in /WEB-INF/libfolder of your webapp or perhaps in the JRE's /libfolder. You should removethose servletcontainer-specific libraries which are sitting somewhere else in the classpath than in the target runtime itself.

javax.servlet.FilterRegistration是在Servlet的3.0推出。此异常表明您的运行时类路径中仍有旧 Servlet API 版本的库,这些库在类加载中具有优先权。例如,从 Internet 上随机挑选的servlet-api.jar文件位于/WEB-INF/lib您的 webapp文件夹或 JRE/lib文件夹中。您应该删除那些位于类路径中而不是目标运行时本身中的特定于 servletcontainer 的库。

If you did this to circumvent compilation problems, then you should instead have taken the target runtime's libraries in the classpath. In for example Eclipse, you can do it in Target Runtimesection of project's properties. See also How do I import the javax.servlet API in my Eclipse project?

如果您这样做是为了规避编译问题,那么您应该在类路径中使用目标运行时的库。例如在 Eclipse 中,您可以在项目属性的Target Runtime部分中执行此操作。另请参阅如何在我的 Eclipse 项目中导入 javax.servlet API?

回答by Ravi Macha

When you use SBT, FilterRegistration class is present in 3.0 and also if you use JETTY Or Java 8 this JAR 2.5 it automatically adds as dependency,

当您使用 SBT 时,FilterRegistration 类存在于 3.0 中,而且如果您使用 JETTY 或 Java 8 这个 JAR 2.5,它会自动添加为依赖项,

Fix: Servlet-api-2.5 JAR was the mess there, I resolved this issue by adding servlet-api-3.0 jar in dependencies,

修复:Servlet-api-2.5 JAR 在那里很混乱,我通过在依赖项中添加 servlet-api-3.0 jar 解决了这个问题,

enter image description here

在此处输入图片说明