Tyrus – simple java application – implementation class not found
•
Java
I am developing simple applications to help me learn the concepts of websocket and Tyrus in Java This is my server (server endpoint) and my POM XML and my client and POM xml:
Server side
@ServerEndpoint(value="/websocket/{client-id}") public class MyServerEndpoint { private static final LinkedList<Session> clients = new LinkedList<Session>(); @OnOpen public void onOpen(Session session) { clients.add(session); } @OnMessage public void onMessage(String message,@PathParam("client-id") String clientId) { for (Session client : clients) { try { client.getBasicRemote().sendObject(clientId+": "+message); } catch (IOException e) { e.printStackTrace(); } catch (EncodeException e) { e.printStackTrace(); } } } @OnClose public void onClose(Session peer) { clients.remove(peer); } }
POM xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>TyrusJacpFXServer</groupId> <artifactId>TyrusJacpFXServer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <scope>compile</scope> <version>1.0</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
client
MyClient.java @ClientEndpoint public class MyClient { @OnOpen public void onOpen(Session session) { System.out.println("Connected to endpoint: " + session.getBasicRemote()); try { session.getBasicRemote().sendText("Hello"); } catch (IOException ex) { } } @OnMessage public void onMessage(String message) { System.out.println(message); } @OnError public void onError(Throwable t) { t.printStackTrace(); } }
App. java
public class App { public Session session; protected void start() { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); String uri = "ws://localhost:8080/TyrusJacpFXClient/websocket/desktop-client"; System.out.println("Connecting to " + uri); try { session = container.connectToServer(MyClient.class,URI.create(uri)); } catch (DeploymentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String args[]){ App client = new App(); client.start(); BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); String input = ""; try { do{ input = br.readLine(); if(!input.equals("exit")) client.session.getBasicRemote().sendText(input); }while(!input.equals("exit")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
POM xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>TyrusJacpFXClient</groupId> <artifactId>TyrusJacpFXClient</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <scope>compile</scope> <version>1.0</version> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-client</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly</artifactId> <version>1.1</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
I received this error:
Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class. at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73) at org.hwc.App.start(App.java:25) at org.hwc.App.main(App.java:40)
Can someone help me see the problem? The server is running in GlassFish 4.0
Solution
You have a dependency problem This:
<dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly</artifactId> <version>1.1</version> </dependency>
Is not correct You need to use [1] (grizzly bear based client):
<dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-client</artifactId> <version>1.8.3</version> </dependency>
Or [2] (JDK 7 NiO client impl)
<dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-jdk-client</artifactId> <version>1.8.3</version> </dependency>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码