Nova
Nova

Reputation: 423

ClassNotFoundException with ResteasyClientBuilder (JBOSS)

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

Upvotes: 2

Views: 10333

Answers (3)

CyberWasp
CyberWasp

Reputation: 361

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

Upvotes: 1

Francesco
Francesco

Reputation: 1802

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.

Upvotes: 0

gladiator
gladiator

Reputation: 1363

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.

Upvotes: 0

Related Questions