websocket在jetty中报错java.lang.ClassNotFoundException: com.cn.test.chapter2.websocket.ReverseAjaxServlet

hello2008world 发布于 2013/01/30 17:15
阅读 5K+
收藏 0

我用的jetty8.0   jdk7,下边是报错信息

Running Jetty 8.0.1.v20110908
ParentLoaderPriority enabled
Enable config class:runjettyrun.webapp.RJRWebInfConfiguration
Enable config class:org.eclipse.jetty.webapp.WebXmlConfiguration
Enable config class:runjettyrun.webapp.RJRMetaInfoConfiguration
Enable config class:org.eclipse.jetty.webapp.FragmentConfiguration
Enable config class:runjettyrun.annotation.RJRAnnotationConfiguration
Enable config class:org.eclipse.jetty.webapp.JettyWebXmlConfiguration
Enable config class:org.eclipse.jetty.webapp.TagLibConfiguration
ProjectClassLoader: entry=F:\mytest\reverse_ajax\WebContent\WEB-INF\classes
ProjectClassLoader: entry=F:\mytest\reverse_ajax\WebContent\WEB-INF\lib\asm-3.3.jar
ProjectClassLoader: entry=F:\mytest\reverse_ajax\WebContent\WEB-INF\lib\jettison-1.3.2.jar
init scanning folders...
add to scan list:F:\mytest\reverse_ajax\WebContent\WEB-INF\classes
add to scan list:D:\reverse_ajax\WebContent\WEB-INF\web.xml
add to scan list:D:\reverse_ajax\WebContent\WEB-INF\websocket.xml
add to scan list:D:\reverse_ajax\WebContent\WEB-INF\Copy of web.xml
Starting scanner at interval of 5 seconds.
2013-01-30 17:04:06.692:INFO:oejs.Server:jetty-8.0.1.v20110908
2013-01-30 17:04:07.339:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/reverse_ajax,[file:/F:/mytest/reverse_ajax/WebContent/]}
2013-01-30 17:04:07.339:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/reverse_ajax,[file:/F:/mytest/reverse_ajax/WebContent/]}
2013-01-30 17:04:07.339:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/reverse_ajax,[file:/F:/mytest/reverse_ajax/WebContent/]}
2013-01-30 17:04:07.342:WARN:oejs.Holder:
java.lang.ClassNotFoundException: com.cn.test.chapter2.websocket.ReverseAjaxServlet
 at runjettyrun.ProjectClassLoader.loadClass(ProjectClassLoader.java:94)
 at org.eclipse.jetty.util.Loader.loadClass(Loader.java:92)
 at org.eclipse.jetty.util.Loader.loadClass(Loader.java:71)
 at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:92)
 at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:255)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:776)
 at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:255)
 at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1212)
 at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
 at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
 at org.eclipse.jetty.server.Server.doStart(Server.java:262)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at runjettyrun.Bootstrap.main(Bootstrap.java:80)
2013-01-30 17:04:07.343:WARN:/reverse_ajax:unavailable
javax.servlet.UnavailableException: com.cn.test.chapter2.websocket.ReverseAjaxServlet
 at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:98)
 at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:255)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:776)
 at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:255)
 at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1212)
 at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:610)
 at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:453)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
 at org.eclipse.jetty.server.Server.doStart(Server.java:262)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
 at runjettyrun.Bootstrap.main(Bootstrap.java:80)
2013-01-30 17:04:07.477:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080 STARTING

 

下边是代码,各位大牛帮忙给看看

package com.cn.test.chapter2.websocket;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jettison.json.JSONArray;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

/**
 * @author Mathieu Carbou (mathieu.carbou@gmail.com)
 */
public final class ReverseAjaxServlet extends WebSocketServlet {

    private final Endpoints endpoints = new Endpoints();
    private final Random random = new Random();
    private final Thread generator = new Thread("Event generator") {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(random.nextInt(5000));
                    endpoints.broadcast(new JSONArray().put("At " + new Date()).toString());
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    };

    @Override
    public void init() throws ServletException {
        super.init();
        generator.start();
    }

    @Override
    public void destroy() {
        generator.interrupt();
        super.destroy();
    }
	
    @Override
	public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
        return endpoints.newEndpoint();
    }

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().write("11111");
	}
    
    
    
    

}

 

 

package com.cn.test.chapter2.websocket;

import org.eclipse.jetty.websocket.WebSocket;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Mathieu Carbou (mathieu.carbou@gmail.com)
 */
final class Endpoints {
    private final Queue<Endpoint> endpoints = new ConcurrentLinkedQueue<Endpoint>();

    void broadcast(String data) {
        for (Endpoint endpoint : endpoints) {
        	endpoint.onMessage(data);
        }
    }

    void offer(Endpoint endpoint) {
        endpoints.offer(endpoint);
    }

    void remove(Endpoint endpoint) {
        endpoints.remove(endpoint);
    }

    public WebSocket newEndpoint() {
        return new Endpoint(this);
    }
}

 

 

 

package com.cn.test.chapter2.websocket;

import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.codehaus.jettison.json.JSONArray;
import org.eclipse.jetty.websocket.WebSocket;

/**
 * @author Mathieu Carbou (mathieu.carbou@gmail.com)
 */
class Endpoint implements WebSocket.OnTextMessage  {

	protected Connection _connection;
	
	private Endpoints endpoints;
	
	private static int clientCounter = 0;
    private int clientId = clientCounter++;
	
	public Endpoint(Endpoints endpoints) {
		this.setEndpoints(endpoints);
	}
	
	@Override
	public void onClose(int code, String message) {
		this.endpoints.remove(this);
	}

	@Override
	public void onOpen(Connection connection) {
		_connection = connection;
		try {
			this._connection.sendMessage(new JSONArray().put("ClientID = " + clientId).toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        endpoints.offer(this);
	}

	@Override
	public void onMessage(final String data) {
		this.endpoints.broadcast(data);
	}

	public Endpoints getEndpoints() {
		return endpoints;
	}

	public void setEndpoints(Endpoints endpoints) {
		this.endpoints = endpoints;
	}
	
	


}

 

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>reverse_ajax</display-name>
  
  <servlet>
        <servlet-name>events</servlet-name>
        <servlet-class>com.cn.test.chapter2.websocket.ReverseAjaxServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>events</servlet-name>
        <url-pattern>/async</url-pattern>
    </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 


以下是问题补充:

@hello2008world:解决了是没有在项目中添加jar包,jetty-util-8.0.4.v20111024.jar和jetty-websocket-8.0.4.v20111024.jar。现在可以了。 (2013/01/31 11:25)
加载中
返回顶部
顶部