how*_*unt 0 java client websocket maven tyrus
我是Java的新手,但我必须用它来做一个与WebSocket相关的小项目.
所以,我在我的CentOS 7上在VirtualBox中安装了JDK 1.8.0和NetBeans 8.1.
我加入了泰鲁斯-独立客户端- JDK 1.12插件在pom.xml中做出独立的WebSocket客户端,并建立了良好的.但是,我遇到了以下错误:
[root@cet7 ~]# java -jar "/root/NetBeansProjects/Switchclient/target/Switchclient-1.0-SNAPSHOT.jar"
Exception in thread "main" java.lang.NoClassDefFoundError: javax/websocket/ContainerProvider
at org.sample.switchclient.Switchclient.main(Switchclient.java:21)
Caused by: java.lang.ClassNotFoundException: javax.websocket.ContainerProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
[root@cet7 ~]# java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
Run Code Online (Sandbox Code Playgroud)
我没有更多的搜索和发现的"容器实现的完全限定类名ContainerProvider
必须在上市META-INF /服务/ javax.websocket.ContainerProvider为在执行JAR文件的文件" ServiceLoader
,根据Oracle文档API.所以,我将serviceloader-maven-plugin添加到pom.xml中.结果是它确实生成了META-INF/services/javax.websocket.ContainerProvider文件,但没有任何内容,并且运行时错误继续存在.我尝试手动修改内容并将其重新打包到JAR中,但它没有奏效:
我已经附加了Java文件和pom.xml.我已经工作了几个小时,并且不知道问题是什么,因此对此线程的任何回复都将不胜感激.
非常感谢你.
=========== LIST1:pom.xml ===========
<?xml version="1.0" encoding="UTF-8"?>
<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>org.sample</groupId>
<artifactId>Switchclient</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.sample.switchclient.Switchclient</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>eu.somatik.serviceloader-maven-plugin</groupId>
<artifactId>serviceloader-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<services>
<param>javax.websocket.ContainerProvider</param>
</services>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client-jdk</artifactId>
<version>1.12</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Run Code Online (Sandbox Code Playgroud)
=========== LIST2:Switchclient.java ===========
package org.sample.switchclient;
import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
@ClientEndpoint
public class Switchclient {
@OnMessage
public void onRemoteMessage (String message) {
System.out.println("Received msg: "+message);
}
public static void main(String[] args) {
WebSocketContainer container = null;
Session session = null;
try{
container = ContainerProvider.getWebSocketContainer();
session = container.connectToServer (Switchclient.class, URI.create("ws://localhost:8080/Switchserver/"));
}catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定到底是什么导致了这个问题,因为我一直在尝试,但在过去的一天里问题不断出现。但最后是:
客户端依赖项:
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-client-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-container-grizzly-client</artifactId>
<version>1.12</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
乍一看似乎 javax.websocket-client-api 应该足够了,但最后 cyber 说 ContainerProvider 不是障碍。
然后,一切就OK了。(对于与我原来的帖子不同的java代码,我尝试了很多包含源代码的方法,但是代码本身与这里的匹配并不重要,而环境设置 很重要。然而,它们主要基于Tyrus 1.9 用户指南的示例。)
通过 maven 从 NetBeans 运行是可以的,但是当我使用“java -jar Switchclient.jar”时,相同/类似的问题跳出来,说“Endpoint”有问题。
最后(作为最后一次尝试)我将类路径中包含的所有 tar 文件(witch 是由 maven-jar-plugin 通过指定“ <addClasspath>true<addClasspath>
”生成的)复制到一个目录中,并将生成的 jar 文件复制到其中,然后它起作用了:
[root @cet7 needjars]# ls
grizzly-framework-2.3.22.jar tyrus-client-1.12.jar
grizzly-http-2.3.22.jar tyrus-container-grizzly-client-1.12.jar
grizzly-http-server-2.3。 22.jar tyrus-core-1.12.jar
javax.websocket-api-1.1.jar tyrus-spi-1.12.jar
javax.websocket-client-api-1.1.jar tyrus-standalone-client-1.12.jar
Switchclient-1.1- SNAPSHOT.jar
[root@cet7 needjars]# java -jar Switchclient-1.1-SNAPSHOT.jar
收到消息:Hello world
就是这样,肮脏但有效。我又开始了。再说一次,我对java真的很陌生(那些非硬技术人员之一,只要在需要时就拿起它);它向我展示了基于社区的开发的复杂性,特别是在技术相对较新的情况下。到处都是依赖和陷阱。我想这就是大自然的一部分......
基本上,Tyrus需要Java EE.这就是你必须列出很多依赖关系的原因pom.xml
.如果您使用Java SE并希望保持项目较小,请使用另一个仅依赖于Java SE的WebSocket客户端库.例如,nv-websocket-client(我的).
只需添加以下依赖项pom.xml
,
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>1.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后尝试:
import com.neovisionaries.ws.client.*;
public class Switchclient
{
public static void main(String[] args) throws Exception
{
WebSocket websocket = new WebSocketFactory()
.createSocket("ws://localhost:8080/Switchserver/")
.addListener(new WebSocketAdapter() {
@Override
public void onTextMessage(WebSocket ws, String message) {
System.out.println("Received msg: " + message);
}
})
.connect();
// Don't forget to call disconnect() after use.
// websocket.disconnect();
}
}
Run Code Online (Sandbox Code Playgroud)