2

Similar questions have been posted here and here but the former proposes solutions only to NoSuchMethodError and the latter doesn't even mention ResteasyClientBuilder, which is precisely what causes my error whenever I try to call a get method from my client.

I'm using WildFly, Maven, RESTEasy/JBoss. I can successfully run my WildFly server using standalone in command prompt, and use maven to deploy a war file to the WildFly server, and use get methods in a browser/Postman to receive results. However if I try to call the exact same get method from within my client code, I get the error below. It is caused by ResteasyClientBuilder.

What is wrong? I have minimal dependencies and plugins in my pom and the server code works, but the client does not. My pom uses versions "3.0.19.Final" because that's the jar version for resteasy-jaxrs-3.0.19.Final.jar in C:\Users\ME\Documents\Wildfly\wildfly-10.1.0.Final\modules\system\layers\base\org\jboss\resteasy\resteasy-jaxrs\main

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/resteasy/client/jaxrs/ResteasyClientBuilder
    at com.sample.ClientDemo.main(ClientDemo.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>rest-demo</artifactId>
    <version>1.0.SNAPSHOT</version>
    <packaging>war</packaging>
    <name>rest-demo</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.19.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.19.Final</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.1.0.Alpha11</version>
                <configuration>
                    <name>rest-demo.war</name>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

server code (HelloWorld.java)

import javax.ws.rs.*;

@Path("tutorial")
public class HelloWorld
{
    @GET
    @Path("helloname/{name}")
    public String hello(@PathParam("name") final String name) {
        return "Hello " +name;
    }

}

client code (ClientDemo.java)

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import javax.ws.rs.core.Response;
public class ClientDemo {

    public static void main(String[] args) {
        ResteasyClient client = new ResteasyClientBuilder().build();  // <-- Error occurs here
        ResteasyWebTarget target = client.target("http://localhost:8080/rest-demo/rest/tutorial/helloname/john");
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        response.close();
    }
}

Edit Configurations in IntelliJ

The error remains even after explicilty referencing the client jar. What is wrong?

enter image description here

Community
  • 1
  • 1
Nova
  • 423
  • 1
  • 10
  • 36
  • I can't reproduce your error using my dummy project based on stuff you put here (I also used wildfly 10.1.0.Final). Are there differences between mine and your project? Here is the code: https://github.com/fpezzati/NovaRest – Francesco Nov 08 '16 at 12:41
  • Ok, I reproduced your github hierarchy/code identically within a new Maven project. I even moved `HelloWorld.java` and `JaxRsActivator` into a `client` package within `edu.nova.client` and moved `ClientDemo.java` into the `rest` package. But when I right-click on ` ClientDemo.java` and click 'Run', the original error remains: `Caused by: java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder` . Do I have to do something with `.m2` repo or something? What are you doing differently? :( Here's what I see: http://imgur.com/a/QG2tx – Nova Nov 08 '16 at 13:00
  • I perform the following process in terminal: `mvn clean compile` and then `mvn package` and then `mvn wildfly:deploy`. I then right-click on the main class and select Run, which gives the error. The exact same `GET` request works fine in Postman, but fails in IntelliJ. – Nova Nov 08 '16 at 13:04
  • I also add a junit test to the project which do exactly what your client does.Can you run it succesfully? – Francesco Nov 08 '16 at 13:22
  • No, it fails on `mvn wildfly:deploy`. You can see the full stacktrace here: http://pastebin.com/se3h08kQ. I've also added the surefire dependency with no luck. I tried deleting my WildFly folder, and re-extracting the zip, so now my folder `C:\Users\ME\Documents\Wildfly\wildfly-10.1.0.Final\modules\system\layers\base\org\jboss\resteasy` has these files: http://imgur.com/a/uezyn .... perhaps something is wrong here? – Nova Nov 08 '16 at 13:35

3 Answers3

1

You need to divided maven project on two parts. One for client demo. Another for war. In war's pom add dependency only on org.jboss.resteasy:jaxrs-api and make it provided. In client pom add dependency on org.jboss.resteasy:resteasy-client WITHOUT provided (maven exec plugin don't include provided dependency in classpath) I put my reasteasy demo projects on github (server, client). It test additional funciton and need more dependency then in your case. Client can work through mvn exec:exec

CyberWasp
  • 361
  • 3
  • 13
  • thanks for the input. The problem is that when I remove the scope "provided", I can successfully use `mvn clean compile` but when I try `mvn wildfly:deploy` I get the error `Could not find constructor for class: org.jboss.resteasy.core.AsynchronousDispatcher"` . According to [this solution](http://stackoverflow.com/a/15603862), it says to ADD the scope "provided". ! So I'm confused. As for the jboss .xml file, it doesn't exist and I need Maven to create it, b/c if I manually create it it doesn't get deployed with `mvn wildfly:deploy`. – Nova Nov 06 '16 at 18:45
  • The post [here](http://stackoverflow.com/a/26507850/3851150) says that WildFly doesn't generate these files automatically, and the plugin it suggests seems cumbersome. Why is this so difficult and confusing? Please help – Nova Nov 06 '16 at 18:46
  • this file add to war or ear: ... jboss-deployment-structure.xml is a JBoss specific deployment descriptor that can be used to control class loading in a fine grained manner. It should be placed in the top level deployment, in META-INF (or WEB-INF for web deployments)... – CyberWasp Nov 06 '16 at 19:35
  • and it deployed with it – CyberWasp Nov 06 '16 at 19:36
  • Thanks CWasp, I'm just a little confused as to what you're telling me to do. Does your last comment contain what I should write inside `jboss-deployment-structure.xml` inside WEB-INF? If so, if I manually add this .xml file into that folder, then I can't use Maven to deploy anymore, because the maven command `mvn wildfly:deploy` overwrites the my project's `/target`, and therefore also the WEB-INF folder and the .xml file. I need to integrate that command you provided INTO Maven. Is this possible? Anything helps, thanks. – Nova Nov 06 '16 at 19:50
  • 1
    Yes. In WEB-INF folder, but not to target folder. In src folder make folder resource and put WEB-INF/jboss-deployment-structure.xml. in it. When maven build your project WEB-INF folder will be include into result WAR. – CyberWasp Nov 06 '16 at 20:30
  • 1
    sample from github https://github.com/andhou75/myschedule.experiment/blob/c21be4d4cec9513d59c4e5328b1bbe6ae7808ae0/quartz18ee/quartz18ee-job/src/main/resources/META-INF/jboss-deployment-structure.xml – CyberWasp Nov 06 '16 at 20:37
  • Ok, I have created `jboss-deployment-structure.xml` inside `C:\Users\ME\Documents\rest-demo\src\resources\WEB-INF`, and included what you told me to. But when I do `mvn wildfly:deploy` it creates `rest-demo-1.0.SNAPSHOT.war` within `rest-demo/target` BUT this war file does not contain the `jboss-deployment-structure.xml` file anywhere. Why not? Am I missing a Maven plugin or something? It's as if the /src/resources/WEB-INF directory isn't considered. My ClientDemo.java code still returns the original error even with `` in the xml too – Nova Nov 06 '16 at 21:33
  • I added the `resources` plugin to maven and specified the `src/resources/` folder so now the `.xml` is being saved into the `war`, but I'm still getting the original Client error. I will report back if I figure something out... thoughts? – Nova Nov 06 '16 at 22:35
  • src/main/resources/META-INF/jboss-deployment-structure.xml sorry for inaccuracy – CyberWasp Nov 07 '16 at 05:12
  • or src/main/webapp/WEB-INF/jboss-deployment-structure.xml for war – CyberWasp Nov 07 '16 at 07:11
  • 1
    Ops... Error on client :(. Forget for jboss-deployment-structure.xml. You need simple add to classpath jar's from resteasy-client dependencies when running ClientDemo.main. Simple way to do this maven-exec-plugin http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html – CyberWasp Nov 07 '16 at 20:38
  • I'm sorry, but are you saying to completely disregard the jboss `.xml` file? If so, can you please clarify what you mean by "You need simple add to classpath jar's from resteasy-client dependencies when running ClientDemo.main". I don't understand. You've proposed the `maven-exec-plugin` as a solution which leaves me confused about (1) Will I always have to execute my program from maven now?? I'd prefer not to. (2) What should `pom.xml` look like? Can you edit your answer to show me your solution? I'm confused which dependencies should be in the pom, and whether to be scope provided or not. – Nova Nov 08 '16 at 00:45
  • There are many contradicting posts on StackOverflow about this topic. For instance, the solution [here](http://stackoverflow.com/a/24828201) says scope `provided` should be removed, whereas [this post](http://stackoverflow.com/a/15603862/3851150) says to keep it. I've made no progress in 3 days... what should my pom look like exactly? Assuming I don't change any other files. If you can provide a working pom I'll give you all the points I can!! – Nova Nov 08 '16 at 00:49
  • Here is my dependency tree: http://imgur.com/a/l2grN. I've also followed [this post](http://stackoverflow.com/a/26678738/3851150) and it gives the same error, yet it works for everyone else :( – Nova Nov 08 '16 at 01:06
  • I really, really appreciate all the help you've provided. The github server code you created uses `web.xml.` which requires jersey. I do *not* want to use Jersey since I'm using WildFly already, and want to use WildFly/RESTEasy exclusively. I have no problem getting the client to work if I use the `web.xml` file, but I've read that this is not necessary if you instead use `JaxRsActivator` instead. I'm not sure how to adapt your answer so that no Jersey dependencies are required. I've read GlassFish has stopped commercial support so I don't want to even look at it. – Nova Nov 08 '16 at 12:40
  • Why you decide that web.xml using jersey? Wildfly just create JaxRsActivator by description from web.xml. In client demo jersey need for testing working of jersey through http-proxy. It remain from may previous demo of rest client :). I clear sources from this trash and pull to github. If you won't use maven to exec you may include [dependency inside client jar](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – CyberWasp Nov 08 '16 at 15:10
  • I try run Francesco client by IDEA. Same error java.lang.NoClassDefFoundError: org/jboss/resteasy/client/jaxrs/ResteasyClientBuilder. My final verdict need separate project on two - one for rest client (without provided scope ) and one for war with rest service (with provided scope) – CyberWasp Nov 08 '16 at 16:10
  • Are you saying that my *only* option is to split my project in two? That seems overly complex for such a simple issue... I don't understand why the client jar can't be found like all the others. Is this not a problem with a classpath variable or something, which seems like Francesco is implying? I need to integrate RESTEasy into my other project so I' prefer not to change anything at the project level. Why can't I use a single project? – Nova Nov 08 '16 at 18:27
  • Your code - your rule :) But a bit of philosophy: while server part is simple and not contains many dependencies it seems logical combine client and server. But when system will be more complicate server dependency impede using this jar by client. Example - your rest service get from db list of user name and return it to client. When client combine with server system (which used this client) need all db staff (db driver, may be need connection setting and something more) and rest client staff. If client is separated then it need rest client staff only. – CyberWasp Nov 08 '16 at 20:28
  • 1
    if your want single module - remove provided scope and exclude resteasy jar by packagingExcludes configuration in war plugin. – CyberWasp Nov 08 '16 at 20:30
  • 1
    org.apache.maven.plugins maven-war-plugin 2.3 WEB-INF/lib/* false – CyberWasp Nov 08 '16 at 20:30
  • I try it on Francesco demo client. Seams it work when run from Idea – CyberWasp Nov 08 '16 at 20:32
  • Ok, I'll try now and report back. :) – Nova Nov 08 '16 at 20:32
  • SOLVED!! Thank you CWasp, you've been instrumental in me solving this. I really appreciate your patience with me, and congratulations on your start to StackOverflow :) You get the bounty. I'm curious though... will this `` approach be suitable for commercial distribution? In other words, if I distribute my app to end users, will the server-client connection work as expected on their machine too? Or is this only for development? Perhaps separating them into separate projects is better for this too. I'm just confused how I'd integrate 3 projects (main source, client, & server) – Nova Nov 08 '16 at 20:44
  • It says I have to wait 4 hours to award the bounty. I will be back I promise. Also, since we've spent so much time on this... can you please explain why this package exclusion solved my problem? Other people may benefit too. Thank you :) – Nova Nov 08 '16 at 20:46
  • Mainly it will work from Idea. Idea don't include in classpath (when run with its launcher) dependencies with scope provided. By she don't process packagineExcludes. Replace one with another and we fool IDEA :). May fool exec maven plugin too (I don't try). – CyberWasp Nov 08 '16 at 21:17
  • Commercial distribution my use it but with caution. If commercial distribution use wildfly it must include resteasy-client module in its jboss-deployment-structure and exclude all dependencies resteasy-client which add by your client (through packagineExcludes or exclusions section of dependency entry of pom) If commercial distribution is simple console application it must add to classpath all recursive dependencies for reasteasy-client. – CyberWasp Nov 08 '16 at 21:17
  • And from my point of view I will spit on two subproject :). [One big project with contains client and war](http://www.codetab.org/apache-maven-tutorial/maven-multi-module-project/) – CyberWasp Nov 08 '16 at 21:22
  • Helpful tutorial, it looks simpler than I thought it would be. But I'm concerned about the commercial distribution aspect, you said do it with caution. Can you recommend a more reliable approach so that end users don't experience the same error I did? I do not want to use `jboss-deployment-structure.xml` if it isn't reliable. Is splitting the project into multiple modules a more reliable approach, in terms of distribution? Like what you said `My final verdict need separate project on two - one for rest client (without provided scope ) and one for war with rest service (with provided scope)` ? – Nova Nov 08 '16 at 21:41
  • If I split my project into multiple modules, I assume the my current source code (thousands of lines) would be considered the "parent" module, with the parent `pom.xml`. The client and server would be the other 2 modules. Client will *not* have the provided scope in it's pom for any `rest-easy` dependencies, but server *would* have scope provided. Is this more reliable than `jboss-deployment-structure.xml` for end users? I want a solid REST communication between server-client to access my sensitive functions/code from my server, and access a DB. Thank you :)) – Nova Nov 08 '16 at 21:44
  • 1
    Yes. Splitting the project into multiple modules a more reliable approach, in terms of distribution and support. In case of demo we can write META-INF/lib/* in packagingExcludes, but you can have another dependencies in lib and your must specify explicitly all dependency of resteasy in packagingExcludes. When resteasy change version dependecies can changes and your need update packagingExcludes manually. – CyberWasp Nov 09 '16 at 06:21
  • Makes sense! CWasp, I want to again thank you for your committment to this issue, you've provided me with a considerable amount of value. For that I award you the bounty of 100 points. Congrats and take care :) – Nova Nov 09 '16 at 13:10
0

can u explain how are you running the client code from eclipse or cmd . In case it is from eclipse just add maven dependencies to library and run a java program or in case from cmd then add maven dependencies to class path.

gladiator
  • 1,249
  • 8
  • 23
  • I use IntellIJ Community Edition + Terminal to run the commands `mvn clean compile` and then `mvn wildfly:deploy`. This successfully deploys my server, but when I right-click on my main class (ClientDemo.java) and click 'Run', that's where the client error occurs. I am using the `JaxRsActivator` and do not want to use Jersey/GlassFish dependencies at all. Take a look at my `pom.xml` file, what am I missing? Can you show me what I should make it look like, specifically? Thank you! – Nova Nov 08 '16 at 12:42
  • I am using eclipse ,maven dependencies same as yours and added maven dependencies to build path .Yes one more thing remove jboss from build path (server library if added) – gladiator Nov 09 '16 at 06:27
0

Wildfly is ok. I think it's your client's classpath that is not using all the jars it needs. Here is the command I use to run the ClientDemo class:

java -cp "/.../.m2/repository/org/jboss/logging/jboss-logging/3.1.4.GA/jboss-logging-3.1.4.GA.jar:/.../.m2/repository/org/jboss/resteasy/resteasy-client/3.0.19.Final/resteasy-client-3.0.19.Final.jar:/.../.m2/repository/org/jboss/resteasy/resteasy-jaxrs/3.0.19.Final/resteasy-jaxrs-3.0.19.Final.jar:/.../.m2/repository/org/jboss/spec/javax/ws/rs/jboss-jaxrs-api_2.0_spec/1.0.0.Final/jboss-jaxrs-api_3.0_spec-2.0.0.Final.jar:/.../.m2/repository/org/jboss/spec/javax/annotation/jboss-annotations-api_1.2_spec/1.0.0.Final/jboss-annotations-api_1.2_spec-1.0.0.Final.jar:/.../.m2/repository/javax/activation/activation/1.1.1/activation-1.1.1.jar:/.../.m2/repository/org/apache/httpcomponents/httpclient/4.3.6/httpclient-4.3.6.jar:/.../.m2/repository/org/apache/httpcomponents/httpcore/4.3.3/httpcore-4.3.3.jar:/.../.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/.../.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/.../.m2/repository/commons-io/commons-io/2.1/commons-io-2.1.jar:/.../.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/.../Desktop/javax.ws.rs-api-2.0.jar:." edu.nova.client.ClientDemo

Notice that to run it I have to download javax.ws.rs-api-2.0.jar but I in my case Eclipse provide it when I run ClientDemo by the IDE. I don't know IntellJ but check what is in your launch configuration classpath.

Francesco
  • 1,742
  • 5
  • 44
  • 78
  • If I try that same command in IntelliJ's terminal, I get `Error: Could not find or load main class edu.nova.client.ClientDemo`. I'm not certain what you mean by "launch configuration classpath", I've added an image to my original question which shows how I've tried to add the client jar's directory to the `CLASSPATH` environment variable, but the error remains. What should I be doing differently? I need it to work on all user's computers... not just my development environment. – Nova Nov 08 '16 at 18:17
  • CWasp provided a solution in the comments below which I've accepted tentatively. I can't award the bounty until 4 more hours... but I'm curious what your thoughts are, can you share? (1) CWasp says it's better to separate class/server into 2 separate projects - which can be beneficial with servers with more dependencies, thoughts? (2) What do you think about their solution of the `packagingExcludes` approach in the war plugin? It has solved my problem, but is it suitable for commercial distribution? I need it to be reliable for end users. Thanks for your time :) – Nova Nov 08 '16 at 20:48
  • Well I think your server is ok because it is easy and my browser and Postman says it works. So the problem must be in your client (but not) or how you run your it. That is my basic idea. (1) Server and client are two separate application, it makes sense to have two separate projects. But I think your client is not a real "application" but a sort of unit test. (2) It looks too complicated to me. You are changing how your project is build because of a not working client (but browser and Postman works). – Francesco Nov 09 '16 at 09:34