java - ContainerProvider 的独立 Java Websocket 客户端 NoClassDefFoundError

标签 java maven websocket client tyrus

我是 Java 新手,但我必须用它来做一个与 WebSocket 相关的小型项目。

因此,我在 VirtualBox 中的 CentOS 7 上安装了 JDK 1.8.0 和 NetBeans 8.1。

我在 pom.xml 中添加了 tyrus-standalone-client-jdk 1.12 插件来制作独立的 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)

我做了更多搜索,发现“ContainerProvider 的容器实现的完全限定类名必须列在 META-INF/services/javax.websocket.ContainerProvider<根据 Oracle 文档,ServiceLoader API 的实现 JAR 文件中的/em> 文件。因此,我将 serviceloader-maven-plugin 添加到了 pom.xml 中。结果是它确实生成了 META-INF/services/javax.websocket.ContainerProvider 文件,但没有任何内容,并且运行时错误继续存在。我尝试手动修改以下内容并将其重新打包到 JAR 中,但没有成功:

  1. org.glassfish.tyrus.container.inmemory.InMemoryContainerProvider
  2. org.glassfish.tyrus.client.ClientManager

我已附加 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>

==========LIST2:Switchclient.java===========
包 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();
        }
    }
}

最佳答案

基本上,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>

然后尝试:

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();
    }
}

关于java - ContainerProvider 的独立 Java Websocket 客户端 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34108512/

相关文章:

node.js - socket.io 来自一个客户端的所有线程都具有相同的 socket.id

spring - 为什么 Spring SendToUser 不起作用

java - 使用多个 XML 处理器的最佳实践?

java - 不使用 root 进行映射的 Spring Data REST 示例

java - 增加 Eclipse 中的 JRE 内存使用量

Java:如何将我的背景设置为显示按钮的图片

maven - Android Studio - 找不到 intellij-core.jar

rest - 有没有办法从 Web 服务自动生成测试?

maven - 我可以在 Maven 中推迟执行插件后的资源过滤吗?

java - websockets api中.sendText()和System.out.println有什么区别