Jersey 2.0 “Getting Started” guide, mainClass not found

北城以北 提交于 2019-12-09 15:57:04

问题


Hi I am trying to follow the Getting Started guide for Jersey 2.0.

I did steps 1.1 and 1.2 as is. No problem there.

For step 1.3 I had a problem cause maven could not find the javax-annotation 1.2 but I solved it following the advice of another Stackoverflow user and added a repository to my pom.

Somvn clean test passes with no problems, BUT when I try to run mvn clean exec:java I get back

[WARNING] java.lang.ClassNotFoundException: com.example.Main
    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:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:722)

The pom.xml is the one created by the following command:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
-DarchetypeVersion=2.0

where the only addition I 've made is the following:

<repositories>
  <repository>
    <id>java.net.repo</id>
    <url>https://maven.java.net/content/groups/promoted/</url>
    <layout>default</layout>
  </repository>
</repositories>

In case it is of interest this is the output of mvn -version

 Apache Maven 3.0.4 (r1232337; 2012-01-17 10:44:56+0200)
 Maven home: /usr/share/maven
 Java version: 1.7.0_21, vendor: Oracle Corporation
 Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/jre
 Default locale: el_GR, platform encoding: UTF-8
 OS name: "mac os x", version: "10.8.3", arch: "x86_64", family: "mac" 

回答1:


I am able to reproduce your error. This has nothing to do with Jersey; it's an error you're making with Maven. mvn clean exec:java says: "delete all the .class files and then try to run the Main .class file". Since you deleted it, Maven can't find it. Here's a solution:

mvn clean compile exec:java

This should fix the problem in your question. This says "delete all the .class files, recompile them from source and then try to run the Main .class file"

To get past the next error you'll see, delete the <scope>test</scope> line from the client dependency in the pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <scope>test</scope>
</dependency>

To:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
</dependency>


来源:https://stackoverflow.com/questions/16993399/jersey-2-0-getting-started-guide-mainclass-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!