Hello everybody
I'm building a Java EE REST Client following the official J2EE guide. So I wrote these lines of code in a Java Application (I'm using Netbeans):
package org.my.utils; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; public class RESTUtils { public static String RESTRequestGET(String URL, String token) { Client client = ClientBuilder.newClient(); // HERE I GET THE ERROR Response response = client.target(URL) .request(MediaType.APPLICATION_JSON) .header("Content-Type", "application/json") .header("X-Auth-Token", token) .get(); String body = (String) response.getEntity(); client.close(); return body; } }
I call this method in the following code:
String responseBody = RESTUtils.RESTRequestGET(URL, token);
When I run the application I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder
at org.my.utils.RESTUtils.RESTRequestGET(RESTUtils.ja va:33)
at org.my.pack.VMServiceConnector.getResources(VMServ iceConnector.java:31)
at testgetvm.TestGetVM.main(TestGetVM.java:26)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.client.ClientBuilder
at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 25)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 58)
... 3 more
Java Result: 1
It seems that the compiler cannot find the ClientBuilder class even though I can access the ClientBuilder implementation (the compiled one) by Ctrl-clicking on "ClientBuilder" in my source code.
What can I do to solve this annoying problem? I could use the HttpURLConnection class to access the REST service but it's not a Java EE official solution.
Thank you