由以下原因引起的错误:org.glassfish.tyrus.spi.ClientContainer

时间:2016-01-02 10:23:46

标签: java websocket wss

我是java的新手。 我想连接到这样的Web套接字:

import java.net.URI;
import java.net.URISyntaxException;

public class PrimeBot {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        // open websocket
        final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("wss://example.com"));

        // add listener
        clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
            public void handleMessage(String message) {
                System.out.println(message);
            }
        });

        // send message to websocket
        clientEndPoint.sendMessage("{''}");

        // wait 3 seconds for messages from websocket
        Thread.sleep(3000);

    } catch (InterruptedException ex) {
        System.err.println("InterruptedException exception: " + ex.getMessage());
    } catch (URISyntaxException ex) {
        System.err.println("URISyntaxException exception: " + ex.getMessage());
    }
}
}


import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

第二课

@ClientEndpoint
public class WebsocketClientEndpoint {

Session userSession = null;
private MessageHandler messageHandler;

public WebsocketClientEndpoint(URI endpointURI) {
    try {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.connectToServer(this, endpointURI);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

/**
 * Callback hook for Connection open events.
 *
 * @param userSession the userSession which is opened.
 */
@OnOpen
public void onOpen(Session userSession) {
    System.out.println("opening websocket");
    this.userSession = userSession;
}

/**
 * Callback hook for Connection close events.
 *
 * @param userSession the userSession which is getting closed.
 * @param reason the reason for connection close
 */
@OnClose
public void onClose(Session userSession, CloseReason reason) {
    System.out.println("closing websocket");
    this.userSession = null;
}

/**
 * Callback hook for Message Events. This method will be invoked when a client send a message.
 *
 * @param message The text message
 */
@OnMessage
public void onMessage(String message) {
    if (this.messageHandler != null) {
        this.messageHandler.handleMessage(message);
    }
}

/**
 * register message handler
 *
 * @param message
 */
public void addMessageHandler(MessageHandler msgHandler) {
    this.messageHandler = msgHandler;
}

/**
 * Send a message.
 *
 * @param user
 * @param message
 */
public void sendMessage(String message) {
    this.userSession.getAsyncRemote().sendText(message);
}

/**
 * Message handler.
 *
 * @author Jiji_Sasidharan
 */
public static interface MessageHandler {

    public void handleMessage(String message);
}
}

我包括: - gson-2.5.jar - javax.websocket-client-api-1.1.jar - tyrus-client-1.12.jar - tyrus-container-grizzly-client-1.12.jar

仍然收到错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/tyrus/spi/ClientContainer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyContainerProvider.getContainer(GrizzlyContainerProvider.java:53)
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:67)
at WebsocketClientEndpoint.<init>(WebsocketClientEndpoint.java:24)
at PrimeBot.main(PrimeBot.java:10)
Caused by: java.lang.ClassNotFoundException
org.glassfish.tyrus.spi.ClientContainer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more

1 个答案:

答案 0 :(得分:0)

缺少此库:

<dependency>
    <groupId>org.glassfish.tyrus</groupId>
    <artifactId>tyrus-spi</artifactId>
    <version>${project.version}</version>
</dependency>