我是Codenvy的新手,我正在使用基于this code的Jersey开发RESTful服务。当我构建并运行代码时,控制台会返回此错误。
[STDERR]线程“main”中的异常java.lang.NoClassDefFoundError:com / sun / jersey / api / container / grizzly / GrizzlyWebContainerFactory
[STDERR]在com.apiconnect.Main.main(Main.java:22)
[STDERR]引起:java.lang.ClassNotFoundException:com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory
[STDERR] at java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)
java.net.URLClassLoader $ 1.run的[STDERR](URLClassLoader.java:355)
java.security.AccessController.doPrivileged(Native Method)中的[STDERR]
java.net.URLClassLoader.findClass上的[STDERR](URLClassLoader.java:354)
java.lang.ClassLoader.loadClass上的[STDERR](ClassLoader.java:425)
[STDERR] at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
java.lang.ClassLoader.loadClass上的[STDERR](ClassLoader.java:358)
[STDERR] ......还有1个
第22行是:
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
编辑: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/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>
我也在Netbeans中尝试过我的代码并且工作正常。有什么问题,如何解决?
答案 0 :(得分:3)
在Codenvy中,构建和运行流程发生在不同的节点和不同的环境中。这个项目生成/ repo有很多jar和一个无法注入Docker容器的启动脚本(Codenvy runners是基于Docker的)。因此,我建议在相同的环境中,即在运行时中执行构建和运行。你需要做的是创建一个新的跑步者(按钮在跑步者面板上),在那里安装Maven,执行构建并执行启动脚本。您还必须从localhost解除绑定服务(请参见下面的屏幕截图)。您的机器配方如下:
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
这将触发构建并运行服务。看一下跑步者面板底部的URL和端口。我们将Docker中的公开端口映射到运行器实例上的外部端口。每次点击“运行”时,都会随机选择主机和端口。
答案 1 :(得分:0)
您的代码未在类路径中找到com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory
类。尝试添加以下依赖项:
<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>