独立Java WebSocket客户端NoClassDefFoundError by ContainerProvider

2022-08-03 00:00:00 websocket client java maven tyrus

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

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

我在pom.xml中添加了tyrus-stand-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的容器实现的完全限定类名必须列在ServiceLoaderAPI的实现JAR文件的META-INF/services/javax.websocket.ContainerProvider文件中。因此,我将serviceloader-maven-plugin添加到pom.xml中。结果是它确实生成了META-INF/services/javax.websocket.ContainerProvider文件,但没有任何内容,并且运行时错误继续存在。我试图手动修改下面的内容,并将其重新打包到一个罐子中,但不起作用:

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

解决方案

基本上,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();
    }
}

相关文章