Search code examples
javarestjerseyjersey-client

ClientBuilder not found


I am trying to build a jersey client that sends a get request to a RESTful webservice. But i obtain the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder
    at helper.RestClient.<init>(RestClient.java:23)
    at MyTest.main(MyTest.java:11)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.client.ClientBuilder
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 2 more

I use the following gradle dependencies:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile group: 'com.google.actions', name: 'actions-on-google', version: '1.+'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.firebase:firebase-admin:6.14.0'
    compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.31'
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.31'
}

This is the code of my jersey client:

package helper;

import domain.Medication;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

public class RestClient {
    private static final String REST_URI = <URL>
    private Client client;

    public RestClient() {
        client  = ClientBuilder.newClient();
    }

    public Medication fetchMedication(String id, String token) {
        return client
                .target(REST_URI)
                .path("medications/" + id)
                .request(MediaType.APPLICATION_JSON)
                .header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
                .get(Medication.class);
    }
}

Solution

  • It seems there was a problem with gradle in my project. I switched to maven and i got it working.