Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException: javax.ws.rs.client.ClientBuilder

I´m trying to consume a Jersey Rest Service from a Web Application (Dynamic Web App in eclispe) as follows:

Client client = ClientBuilder.newBuilder().build(); // Here is the error

WebTarget target = client.target("url/rest").path("List");

String result = target.request(MediaType.TEXT_PLAIN).get(String.class);

When debugging in the line ClientBuilder.newBuilder() the system throws the ClassNotFoundException: javax.ws.rs.client.ClientBuilder error, but the same code, in a java project works perfect.

Full trace:

javax.servlet.ServletException: Error instanciando clase de servlet test
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
causa raíz

java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder
    test.<init>(test.java:27)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
causa raíz

java.lang.ClassNotFoundException: javax.ws.rs.client.ClientBuilder
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177)
    test.<init>(test.java:27)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
like image 230
Keetah Avatar asked Mar 19 '23 01:03

Keetah


1 Answers

So here it comes ;)

First for the understanding: javax.ws.rs.client is the standard package for RESTFul client implementations. Developers should programm against this interface. At runtime you need an implementation of this interface in your classpath. Thats where projects like resteasy or jersey comes in.

Second: In eclipse the buildpath and classpath settings only efect the environment in eclipse. When you put something in there manualy, it exist only in eclipse.

Third: When you developing a webapplication depending on the Java EE webprofile you have automaticly access to all bundled packages of your application server like tomcat or jboss. If your application uses more packages you have to distribute them together with your application. To do this you can place your third party jars under "WEB-INF/libs" in your project. When it is deployed to your application server, jars inside this folder are automaticly placed in your classpath of your app.

Fourth (about your solution): Putting third party libs in the application server lib or endorsed directy make them accessable to all applications deployed on this application server and adds more complexity into your app server setup and maintenance.

like image 118
Rene M. Avatar answered Mar 21 '23 15:03

Rene M.