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

Embedded Jetty Server not working for Java Vaadin app

我正在使用 Maven 构建一个 vaadin 应用程序,它将使用嵌入式码头服务器 运行。我在网上找到了几个示例,并尽最大努力将我从中学到的知识整合到我的代码中,但它不起作用。

我做了一个EmbeddedJettyclass:

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 class:

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 class 时,我得到了这个堆栈跟踪:

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.access0(URLClassLoader.java:73)
    at java.net.URLClassLoader.run(URLClassLoader.java:367)
    at java.net.URLClassLoader.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.access0(URLClassLoader.java:73)
    at java.net.URLClassLoader.run(URLClassLoader.java:367)
    at java.net.URLClassLoader.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.access0(URLClassLoader.java:73)
    at java.net.URLClassLoader.run(URLClassLoader.java:367)
    at java.net.URLClassLoader.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.access0(URLClassLoader.java:73)
    at java.net.URLClassLoader.run(URLClassLoader.java:367)
    at java.net.URLClassLoader.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.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.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.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.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.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.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 非常陌生,非常感谢任何帮助!

不要在你的项目中添加任意的 jar,那是行不通的。

Use a build tool, (really, can't emphasis this enough!) You have many to choose from.

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

这是怎么回事,您的 class 路径中存在多个版本的码头。

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

正常运行时间 class 是内部 class,对于 a bug fix related to java8 compact3 and android compatibility

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

您需要做的事情:

  1. 使用构建工具!
  2. 从您的 build/runtime.
  3. 中消除暗中拍摄的任意工件参考
  4. 从您的 EmbeddedJetty.java 示例中,您只需要引用 jetty-servlet-${version}.jar(仅此而已。不要引用任何其他内容,请参阅步骤 #4)
  5. 让工件依赖知识发挥作用,构建工具将自动、理智地拉入您需要的其余工件。
  6. 如果您正在使用 jetty-all.jar 工件,请停止在您的项目中使用它 (that's not what that artifact exists for)