Question

I've written some services to provide compilation of latex documents via REST and am quite stuck now that it's kind of finished. :/

While developing it, I was testing it using TestNG and RestAssured and it worked like a charm but now I'm trying to run it on its own. I'm not really sure where the problem is, so I try to paste all essential stuff and explain it a bit.

The problem is that when I let the run the server in a main method all alone, the following exeption will occur. (Bonus question: Can I even run it that way or must if be deployed on an application server as war, could it be also run as jar from the command line simply invoking it with java -jar x.jar?)

Console Output

JAVA_HOME=/usr/lib/jvm/java-7-oracle /usr/local/netbeans-7.4/java/maven/bin/mvn "-Dexec.args=-classpath %classpath de.uniluebeck.compilatex.RestServer" -Dexec.executable=java -Dexec.classpathScope=runtime org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.

--- exec-maven-plugin:1.2.1:exec (default-cli) @ CompiLaTex ---
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/server/ResourceConfig
        at de.uniluebeck.compilatex.RestServer.<init>(RestServer.java:31)
        at de.uniluebeck.compilatex.RestServer.main(RestServer.java:42)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.server.ResourceConfig
        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.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 2 more

This is what the class for the server instanciation looks like:

RestServer.java

public class RestServer {

    static final URI BASE_URI = URI.create("http://localhost:8080/");
    final HttpServer httpServer;

    public RestServer() {
        final ResourceConfig resourceConfig = new ResourceConfig(JobService.class, JobFileService.class, LatexEnvironmentService.class, MultiPartFeature.class);
        httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig);
        System.out.println(String.format("Jersey app started with WADL available at %sapplication.wadl\nTry out %sjobs or %slatex/environments",
                BASE_URI, BASE_URI, BASE_URI));
    }

    public void shutdown() {
        httpServer.shutdownNow();
    }

    public static void main (String[] args){
        RestServer restServer = new RestServer();
        try {
            System.in.read();
        } catch (IOException ex) {
            Logger.getLogger(RestServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        restServer.shutdown();
    }
}

I'm just poking around but maybe there is a problem that I'm using annotations only and have no actual web.xml?

JobService.java

@Path("/jobs")
public class JobService {

    private final RequestHandler requestHandler = new RequestHandler();

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response createJob() {
        return requestHandler.handleCreateJob();
    }

    @GET
    @Path("{jobId}/compile/{latexEnvironment}")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response compile(@PathParam("jobId") final Long jobId, @PathParam("latexEnvironment") final String latexEnvironment){ //, @Suspended final AsyncResponse asyncResponse) {
        return requestHandler.handleCompile(jobId, latexEnvironment);
    }

And this is what the pom.xml looks like:

<?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>de.uniluebeck</groupId>
    <artifactId>CompiLaTex</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>CompiLaTex</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
       <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.0</version>
            <exclusions>
                <!-- Exclude Groovy because of classpath issue -->
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- Needs to be the same version that REST Assured depends on -->
            <version>2.2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.10.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-exec</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.gmbal</groupId>
            <artifactId>gmbal</artifactId>
            <version>4.0.0-b001</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.enunciate</groupId>
            <artifactId>enunciate-core</artifactId>
            <version>1.28</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.uniluebeck.compilatex.RestServer</mainClass>
                        </manifest>
                    </archive>
                </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.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
            <id>eclipselink</id>
            <layout>default</layout>
            <name>Repository for library EclipseLink (JPA 2.1)</name>
        </repository>
    </repositories>
</project>

I'm glad for anyone having a look at this question and just blaze away at it, thanks guys! (Also appreciated any tips or links to other topics what the proper playing of deploying such things is.)

Was it helpful?

Solution

Thanks to @andrei-i that got me on the right path, I found out that the scopes for all jersey dependencies where wrong. Being set as provided they must have been there when testing but not during runtime. I simply deleted all those scopes and it works. Gosh!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top