嵌入式Jetty服务器不适用于Java Vaadin应用程序

时间:2015-11-18 18:21:16

标签: java jetty vaadin embedded-jetty

我正在使用maven构建一个vaadin app,它将使用嵌入式jetty服务器运行。我在网上找到了几个例子,并尽我所能将我从中学到的东西整合到我的代码中,但它没有用。

我做了一个EmbeddedJetty课程:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import com.wndgroup.auction.supportsystem.DashboardServlet;

public class EmbeddedJetty {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(server, "/example");
        handler.addServlet(DashboardServlet.class, "/");
        server.start();
    }
}

这是我的DashboardServlet课程:

import javax.servlet.ServletException;
import com.vaadin.server.VaadinServlet;

@SuppressWarnings("serial")
public class DashboardServlet extends VaadinServlet {

    @Override
    protected final void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(new DashboardSessionInitListener());
    }
}

当我尝试运行EmbeddedJetty类时,我得到了这个堆栈跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/util/component/ContainerLifeCycle
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.wndgroup.auction.supportsystem.EmbeddedJetty.main(EmbeddedJetty.java:10)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.util.component.ContainerLifeCycle
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 49 more

这是导入jetty-util jar后我得到的新堆栈跟踪:

Nov 18, 2015 1:31:58 PM org.eclipse.jetty.util.log.Log initialized
INFO: Logging initialized @225ms
Nov 18, 2015 1:31:58 PM org.eclipse.jetty.server.Server doStart
INFO: jetty-9.2.2.v20140723
Nov 18, 2015 1:31:58 PM org.eclipse.jetty.server.handler.ContextHandler doStart
INFO: Started o.e.j.s.ServletContextHandler@35bbe5e8{/example,null,AVAILABLE}
Nov 18, 2015 1:31:58 PM org.eclipse.jetty.server.AbstractConnector doStart
INFO: Started ServerConnector@880ec60{HTTP/1.1}{0.0.0.0:8080}
Nov 18, 2015 1:31:58 PM org.eclipse.jetty.util.component.AbstractLifeCycle setFailed
WARNING: FAILED org.eclipse.jetty.server.Server@3f3afe78: java.lang.NoClassDefFoundError: org/eclipse/jetty/util/Uptime
java.lang.NoClassDefFoundError: org/eclipse/jetty/util/Uptime
    at org.eclipse.jetty.server.Server.doStart(Server.java:379)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at com.wndgroup.auction.supportsystem.EmbeddedJetty.main(EmbeddedJetty.java:13)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.util.Uptime
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/util/Uptime
    at org.eclipse.jetty.server.Server.doStart(Server.java:379)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at com.wndgroup.auction.supportsystem.EmbeddedJetty.main(EmbeddedJetty.java:13)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.util.Uptime
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

我对Jetty和Vaadin非常陌生,所以任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:3)

不要在项目中添加任意jar,这将永远不会有效。

  

使用构建工具,(实际上,不能强调这一点!)你有很多   可供选择。

     
      
  • Maven
  •   
  • Gradle
  •   
  • Ant + Ivy
  •   
  • Scala / SBT
  •   
  • Buildr
  •   
  • 等...
  •   

发生了什么事情,你的类路径中有多个版本的jetty。

我在您的启动日志中看到9.2.2.v20140723(版本是从不同的地方发现的),并且org.eclipse.jetty.util.Uptime的引用不是introduced until 9.2.6.v20141203

Uptime类是a bug fix related to java8 compact3 and android compatibility的内部类。

但是,您还有一个Server对象尝试使用此类,这意味着您在jetty-server.jar之后的版本中拥有9.2.6.v20141203版本,但jetty-util.jar在它之前的版本。

您需要做什么:

  1. 使用构建工具!
  2. 消除构建/运行时的黑暗中任意工件引用。
  3. 在您的EmbeddedJetty.java示例中,您只需要引用jetty-servlet-${version}.jar(就是它。不要引用任何其他内容,请参阅第4步)
  4. 让工件依赖知识做自己的事情,构建工具将自动,灵活地提取你需要的其他工件。
  5. 如果您使用的是jetty-all.jar工件,请在项目(that's not what that artifact exists for)中退出。