java.lang.ClassNotFoundException: org.eclipse.jett

2019-09-12 02:00发布

问题:

      Server server = new Server(9090);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    // Add the echo socket servlet to the /echo path map
    context.addServlet(new ServletHolder(HelloWorldWebSocket.EchoServlet.class),"/echo");

    try
    {
        server.start();
        context.dumpStdErr();
        server.join();
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block

    }


public class HelloWorldWebSocket
{

    @WebSocket
    public static class EchoSocket
    {
        @OnWebSocketMessage
        public void onMessage(Session session, String message)
        {
            session.getRemote().sendStringByFuture(message);
        }
    }

    @SuppressWarnings("serial")
    public static class EchoServlet extends WebSocketServlet
    {
        @Override
        public void configure(WebSocketServletFactory factory)
        {
            factory.getPolicy().setIdleTimeout(Integer.MAX_VALUE);
            factory.setCreator((WebSocketCreator) this);
        }
    }

}

ERROR:

java.lang.ClassNotFoundException: org.eclipse.jetty.websocket.server.WebSocketServerFactory
    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)
    at org.eclipse.osgi.internal.framework.ContextFinder.loadClass(ContextFinder.java:131)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.eclipse.jetty.websocket.servlet.WebSocketServletFactory$Loader.load(WebSocketServletFactory.java:63)
    at org.eclipse.jetty.websocket.servlet.WebSocketServletFactory$Loader.create(WebSocketServletFactory.java:42)
    at org.eclipse.jetty.websocket.servlet.WebSocketServlet.init(WebSocketServlet.java:128)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:613)
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:469)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:755)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1126)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1060)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:498)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:265)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:243)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)
    at java.lang.Thread.run(Thread.java:745)
2014-10-02 15:09:09,601 WARN  [qtp1366620290-27] org.eclipse.jetty.servlet.ServletHandler  -

回答1:

Download websocket-server.jar and add it to your classpath.