公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了独立Java WebSocket客户端NoClassDefFoundError by ContainerProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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文件,但没有任何内容,并且运行时错误继续存在。我试图手动修改下面的内容,并将其重新打包到一个罐子中,但不起作用:

    org.glassfish.tyrus.container.inmemory.InMemoryContainerProvider
    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();
    }
}

这篇关于独立Java WebSocket客户端NoClassDefFoundError by ContainerProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:ContainerProvider Java NoClassDefFoundError WebSocket 客户端 独立
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定