Angel F.
Angel F.

Reputation: 176

Runtime error in Codenvy Java execution

I am newbie with Codenvy and I am developing a RESTful service using Jersey based on this code. When I build and run the code, the console returns this error.

[STDERR] Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory

[STDERR] at com.apiconnect.Main.main(Main.java:22)

[STDERR] Caused by: java.lang.ClassNotFoundException: com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory

[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

[STDERR] at java.security.AccessController.doPrivileged(Native Method)

[STDERR] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

[STDERR] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

[STDERR] ... 1 more

Line 22 is:

SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);

EDIT: pom.xml is:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.apiconnect</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>apiconnect-example</artifactId> 
<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly</artifactId>
        <version>1.9-ea01</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.9-ea01</version>
    </dependency>
    <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.9-ea01</version>
      </dependency>
    <dependency>
        <groupId>com.sun.grizzly</groupId>
        <artifactId>grizzly-servlet-webserver</artifactId>
        <version>1.9.18-i</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
           <artifactId>maven-jar-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
           <version>2.3.2</version>
                <configuration>
                <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.apiconnect.Main</mainClass>
                        </manifest>
                </archive>
                </configuration>
            </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                    <configuration>
                        <assembleDirectory>target</assembleDirectory>
                        <programs>
                            <program>
                                <mainClass>com.apiconnect.Main</mainClass>
                                <name>app</name>
                            </program>
                        </programs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build></project>

I have also tried my code in Netbeans and works fine. What is the problem and how can I solve it?

Upvotes: 0

Views: 765

Answers (2)

bianchi
bianchi

Reputation: 500

In Codenvy, build and run processes take place on different nodes and different environments. This project produces /repo with quite a few jars and a startup script that is impossible to inject into a Docker container (Codenvy runners are Docker based). Thus, I recommend performing build and run in the same environment, i.e. in the runtime. What you need to do is create a new runner (the button is on the runner panel), install Maven there, perform build and execute startup script. You will also have to unbind the service from localhost (see screenshot below). Your machine recipe will be the following:

FROM codenvy/jdk7
# install Maven
RUN mkdir -p /home/user/maven3 && \
wget -qO- "http://archive.apache.org/dist/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz" | tar -zx --strip-components=1 -C /home/user/maven3
ENV M2_HOME /home/user/maven3
RUN echo "export M2_HOME=$M2_HOME" >> /home/user/.bashrc
ENV PATH $M2_HOME/bin:$PATH
RUN echo "export PATH=$PATH" >> /home/user/.bashrc
#expose port
EXPOSE 9998
#map it to an external port
ENV CODENVY_APP_PORT_9998_HTTP 9998
# add project sources
ADD jax-rs-sample-1.0-SNAPSHOT-jar-with-dependencies_sources_unpack /home/user/app/
# change permissions for project folder
RUN sudo chown -R user:user /home/user/app
# build and run
CMD cd /home/user/app && \
    mvn package -q && \
    sudo chmod a+x /home/user/app/target/bin/app && \
    /home/user/app/target/bin/app 2>&1

This will trigger a build and run the service. Take a look at the URL and port in the bottom of a runner panel. We map an exposed port in Docker to an external port on a runner instance. Hosts and ports are randomly chosen each time you hit Run.

jax-rs

file upload

Upvotes: 3

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

Your code is not finding com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory class in your classpath. Try adding below dependency:

   <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.grizzly</groupId>
        <artifactId>grizzly-servlet-webserver</artifactId>
        <version>1.9.18-i</version>
    </dependency>

Upvotes: 0

Related Questions