我正在尝试为CometD创建一个没有args构造函数的新WebSocketClient

static BayeuxClient newInstace(String url) throws Exception {
    WebSocketClient wsClient = new WebSocketClient(); //exception here!!
    wsClient.start();
    Map<String, Object> options = new HashMap<>();
    ClientTransport transport = new JettyWebSocketTransport(options, Executors.newScheduledThreadPool(2), wsClient);
    BayeuxClient client = new BayeuxClient(url, transport);
    return client;
}


但这会引发运行时异常:

java.util.ServiceConfigurationError: org.eclipse.jetty.websocket.api.extensions.Extension: Provider org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:225)
    at java.util.ServiceLoader.-wrap1(ServiceLoader.java)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:366)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:448)
    at org.eclipse.jetty.websocket.api.extensions.ExtensionFactory.<init>(ExtensionFactory.java:35)
    at org.eclipse.jetty.websocket.client.common.extensions.WebSocketExtensionFactory.<init>(WebSocketExtensionFactory.java:36)
    at org.eclipse.jetty.websocket.client.WebSocketClient.<init>(WebSocketClient.java:117)
    at org.eclipse.jetty.websocket.client.WebSocketClient.<init>(WebSocketClient.java:108)
    at org.eclipse.jetty.websocket.client.WebSocketClient.<init>(WebSocketClient.java:88)
    at org.asd.util.customerSupportChat.LekaneClient.newInstace(LekaneClient.java:40)


这是在Android上发生的

minSdkVersion 21
targetSdkVersion 25


我已经包括了这样的库:

//https://mvnrepository.com/artifact/org.cometd.java/cometd-java-websocket-jetty-client/3.1.2
compile group: 'org.cometd.java', name: 'cometd-java-websocket-jetty-client', version: '3.1.2'


您知道什么地方不对吗,我该如何解决?

---------------编辑----------------

这也在stacktrace中:

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension" on path: DexPathList[[zip file "/data/app/org.asd.debug-2/base.apk"],nativeLibraryDirectories=[/data/app/org.asd.debug-2/lib/x86, /system/lib, /vendor/lib]]

最佳答案

正如sbordet在评论中提到的那样,问题在于缺少依赖项。将此添加到build.gradle可以解决此问题:

compile group: 'org.eclipse.jetty.websocket', name: 'websocket-common', version: '9.2.22.v20170606'


(使用旧版本,因为没有Java 8可用)

不知道为什么它不能自动解决。

关于java - 尝试创建新的Jetty WebSocketClient实例时抛出ServiceConfigurationError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46296514/

10-12 01:41