Jersey ClientBuilder.newClient(): source not found

2019-03-24 19:50发布

问题:

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

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

I added these external 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...:

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.

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.

UPDATE 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$1.run(Unknown Source)
    at java.net.URLClassLoader$1.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

回答1:

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

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



回答2:

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

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



回答3:

Only for gradle users.

Right version of javax.ws.rs-api and jersey-client will resolve the class not found issue.

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'