Java Jersey ClientBuilder.newClient():找不到源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22568354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:32:47  来源:igfitidea点击:

Jersey ClientBuilder.newClient(): source not found

javaeclipserestjersey

提问by Sarah Weinberger

I have an Java 64-bit Eclipse application with Eclipse running on Windows 7 Pro x64.

我有一个 Java 64 位 Eclipse 应用程序,Eclipse 运行在 Windows 7 Pro x64 上。

I downloaded the Jerseybundle, jaxrs-ri-2.7.zip, for client RESTful API access.

我下载了Jersey包 jaxrs-ri-2.7.zip,用于客户端 RESTful API 访问。

I added these external jars (Project | Build Path | Configure Build Path... | Libraries):

我添加了这些外部 jars (Project | Build Path | Configure Build Path... | Libraries):

jaxrs-ri/api/javax.ws.rs-api-2.0.jar
jaxrs-ri/lib/jersey-client.jar
jaxrs-ri/lib/jersey-common.jar

Here is the source:

这是来源:

package prjTestJersey;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

public static void main(String[] args)
{
    try
    {
        Client oClient = ClientBuilder.newClient();

        oClient.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

I receive the following error, when stepping over the first line, Clinet oClient...:

我收到以下错误,当跨过第一行时,Clinet oClient...:

Source not found.

Why the error and how do I fix it? Why "source not found" instead of a real error.

为什么会出现错误,我该如何解决?为什么“找不到源”而不是真正的错误。

Note: I tried copying the 3 jar files to the project's lib folder, but that did not do any good. I am using the Eclipse debugger, so pressing F11 in debug view and then doing an F6 over the line.

注意:我尝试将 3 个 jar 文件复制到项目的 lib 文件夹中,但这没有任何好处。我正在使用 Eclipse 调试器,因此在调试视图中按 F11,然后在线路上按 F6。

UPDATE:

更新:

I tried creating a brand new 32-bit application (WindowBuilder SWT application window) and simply updated main(...), and same problem. That means the problem is platform independent.

我尝试创建一个全新的 32 位应用程序(WindowBuilder SWT 应用程序窗口)并简单地更新 main(...),同样的问题。这意味着问题与平台无关。

UPDATE 2:

更新 2:

The posted answer to try running was not a bad idea. :-) That gave another clue, which I have to track down. Here is the dump.

发布的尝试跑步的答案并不是一个坏主意。:-) 这给出了另一个线索,我必须追查。这里是垃圾场。

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/hk2/utilities/binding/AbstractBinder
    at org.glassfish.jersey.client.ClientConfig.<init>(ClientConfig.java:452)
    at org.glassfish.jersey.client.JerseyClientBuilder.<init>(JerseyClientBuilder.java:94)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at javax.ws.rs.client.FactoryFinder.newInstance(FactoryFinder.java:116)
    at javax.ws.rs.client.FactoryFinder.find(FactoryFinder.java:206)
    at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:86)
    at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:114)
    at AppMain.main(AppMain.java:20)
Caused by: java.lang.ClassNotFoundException: org.glassfish.hk2.utilities.binding.AbstractBinder
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 12 more

FINAL ANSWER: (Thanks to both answers for the help.)

最终答案:(感谢两个答案的帮助。)

I was missing dependencies. The needed list is:

我缺少依赖项。需要的清单是:

swt_win32_x86.jar
api/javax.ws.rs-api-2.0.jar
ext/jersey-guava-2.7.jar
ext/hk2-api-2.2.0.jar
lib/jersey-common.jar
lib/jersey-client.jar

采纳答案by joscarsson

My guess is that "Source not found" simply is a message from Eclipse telling you it cannot debug into Client oClient = ClientBuilder.newClient();as you do not have the source code for the three jars attached in Eclipse. If you just run the program (without debugging) it might very well work. Read more here on how to add source code to jar files in Eclipse: Is there an easy way to attach source in Eclipse?.

我的猜测是“找不到源”只是来自 Eclipse 的一条消息,告诉您它无法调试,Client oClient = ClientBuilder.newClient();因为您没有 Eclipse 中附加的三个 jar 的源代码。如果你只是运行程序(没有调试),它可能会很好地工作。在此处阅读有关如何将源代码添加到 Eclipse 中的 jar 文件的更多信息:是否有一种简单的方法可以在 Eclipse 中附加源代码?.

If it still doesn't work, I would suggest adding allthe jars from the bundle you downloaded to make sure you aren't missing some dependency.

如果它仍然不起作用,我建议从您下载的包中添加所有jar 以确保您没有丢失某些依赖项。

回答by JamesB

You are missing the hk2 api jar which contains the class org.glassfish.hk2.utilities.binding.AbstractBinder

您缺少包含 org.glassfish.hk2.utilities.binding.AbstractBinder 类的 hk2 api jar

http://mvnrepository.com/artifact/org.glassfish.hk2/hk2-api

http://mvnrepository.com/artifact/org.glassfish.hk2/hk2-api

回答by Purushothaman

Only for gradle users.

仅适用于 gradle 用户。

Right version of javax.ws.rs-apiand jersey-clientwill resolve the class not found issue.

权版本javax.ws.rs-apijersey-client将解决找不到类的问题。

compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1-m01'
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.24.1'

回答by Kaji Islam

Add below dependency it will resolve the issue

添加以下依赖项它将解决问题

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-client</artifactId>
  <version>3.0.2.Final</version>
</dependency>