1

我是 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)

我做了更多搜索,发现根据Oracle文档,API的“容器实现的完全限定类名ContainerProvider必须列在实现JAR文件的META-INF/services/javax.websocket.ContainerProvider文件中”。ServiceLoader因此,我将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===========
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();
        }
    }
}
4

2 回答 2

6

基本上,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();
    }
}
于 2015-12-08T05:51:57.440 回答
4

我不确定到底是什么导致了这个问题,因为我一直在尝试,并且在过去的一天中问题不断出现。但最后是它:
客户端依赖项:

        <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>

乍一看似乎 javax.websocket-client-api 应该足够了,但最后网络说 ContainerProvider 不是障碍

然后,一切建好。(与我原来的帖子不同的java代码,我尝试了很多包括源代码,但代码本身并不重要,但环境 设置很重要。但它们主要基于Tyrus 1.9用户指南的示例。)

并且通过 Maven 从 NetBeans 运行是可以的,但是当我去使用“java -jar Switchclient.jar”时,同样/类似的问题跳出来说“端点”的问题。

最后(作为最后一次尝试)我复制了类路径中包含的所有 tar 文件(witch 是由 maven-jar-plugin 通过指定“ <addClasspath>true<addClasspath>”生成的到一个目录中,并将生成的 jar 文件复制到其中,然后它工作:

[root @cet7 requiredjars]# 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 requiredjars]# java -jar Switchclient-1.1-SNAPSHOT.jar
收到消息:Hello world

就是这样,肮脏但有效。我正处于一个新的开始。再说一次,我对 java 真的很陌生(那些非硬技术的人之一,如果需要,只需拿起它);它向我展示了基于社区的开发的共谋性,尤其是在技术相对较新的情况下。依赖和陷阱无处不在。这是我猜想的自然的一部分...

于 2015-12-06T16:03:12.240 回答