2

I'm trying to make Restlet work with Jackson with the following Java code:

public class HelloWorldResource extends ServerResource{
    @Get("json")
    public Todo represent()
    {
        Todo t = new Todo();
        t.setId("1");
        t.setDescription("hello");
        t.setSummary("world");

     return t;
    }
    //...
}

When I run the program I get an error:

java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonFactory

These are the jars I'm using:

enter image description here

Why can't it find the dependencies?

EDIT:

I solved the problem by adding this jar to the classpath.

I'm still interested to know if there are some mistakes/redundancies in the jars I'm adding.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
GionJh
  • 2,742
  • 2
  • 29
  • 68
  • The question you should be asking yourself is: "How do I force the dependencies to be included in my final jar? It sounds like you are trying to run the naked jar file with something like `java -jar your_jar_file.jar` and getting NoClassDefFoundError. Java is tells you the problem: "I'm trying to run the jar, and I don't know what x.y.foobar is!". Stuffing the dependencies into the jar has consequences because it can cause the final jar to be huge in size. See: http://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven – Eric Leschinski Jul 05 '16 at 15:52

1 Answers1

1

You should configure your application using Maven. That way, we will have automatically all required dependencies (included dependencies of dependencies transitively).

In your case, we could use something like that within your file 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>org.restlet</groupId>
    <artifactId>restlet-maven-sample</artifactId>
    <name>${project.artifactId}</name>
    <packaging>war</packaging>
    <version>1.0.0-snapshot</version>

    <properties>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.jackson</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.servlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.xml</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360