[英]Runtime error in Codenvy Java execution
I am newbie with Codenvy and I am developing a RESTful service using Jersey based on this code . 我是Codenvy的新手,并且正在基于此代码使用Jersey开发RESTful服务。 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]线程“主”中的异常java.lang.NoClassDefFoundError:com / sun / jersey / api / container / grizzly / GrizzlyWebContainerFactory
[STDERR] at com.apiconnect.Main.main(Main.java:22) com.apiconnect.Main.main上的[STDERR](Main.java:22)
[STDERR] Caused by: java.lang.ClassNotFoundException: com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory [STDERR]原因:java.lang.ClassNotFoundException:com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory
[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:366) [STDERR],位于java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)
[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:355) [STDERR] at java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)
[STDERR] at java.security.AccessController.doPrivileged(Native Method) java.security.AccessController.doPrivileged上的[STDERR](本机方法)
[STDERR] at java.net.URLClassLoader.findClass(URLClassLoader.java:354) [STDERR],位于java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:425) [STDERR],位于java.lang.ClassLoader.loadClass(ClassLoader.java:425)
[STDERR] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) [STDERR],位于sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:358) [STDERR],位于java.lang.ClassLoader.loadClass(ClassLoader.java:358)
[STDERR] ... 1 more [STDERR] ...还有1个
Line 22 is: 第22行是:
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
EDIT: pom.xml is: 编辑: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>
I have also tried my code in Netbeans and works fine. 我也在Netbeans中尝试过我的代码,并且工作正常。 What is the problem and how can I solve it? 有什么问题,我该如何解决?
In Codenvy, build and run processes take place on different nodes and different environments. 在Codenvy中,构建和运行过程发生在不同的节点和不同的环境中。 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). 这个项目使用很多jar和一个启动脚本(无法注入到Docker容器中)来生成/ repo(Codenvy运行程序基于Docker)。 Thus, I recommend performing build and run in the same environment, ie 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. 您需要做的是创建一个新的运行器(该按钮在运行器面板上),在那里安装Maven,执行构建并执行启动脚本。 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. 查看运行器面板底部的URL和端口。 We map an exposed port in Docker to an external port on a runner instance. 我们将Docker中的裸露端口映射到运行器实例上的外部端口。 Hosts and ports are randomly chosen each time you hit Run. 每次单击运行时,主机和端口都是随机选择的。
Your code is not finding com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory
class in your classpath. 您的代码未在类路径中找到com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory
类。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.