问题
I am running the hello world example.
However, I am using version 2.7.
on maven pom.xml I have
<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>com.mydomain.restful</groupId>
<artifactId>AdvertServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Advert Server</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
</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.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I tried running the servlet from command line and from withing the Server plugin on Eclipse. I get the same error:
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
The weird thing is that I can see the class among the .jar files. The class is there.
I have even tried adding the jar skipping maven, but Tomcat won't see the class.
回答1:
From
Eclipse + tomcat - ClassNotFound exception on deploy
If you have installed both the Maven Eclipse Plugin and the Maven WTP Plugin this is automatic. If you don't have these installed, go ahead and install them and then once Eclipse restarts right-click on the project and do a Maven > Update project.... This will internally change the project configuration so the Maven dependencies are copied to the /WEB-INF/lib folder on your deployment target.
If you don't want to use any of these plug-ins, then you have to go to the Project properties > Deployment Assembly configuration and add your dependencies manually, but again, this is done automatically by these plug-ins.
回答2:
Just a hint...
At the end, what you are creating are good old war files... which should have your libraries under WEB-INF/lib. Have you checked if it is happening? I mean, you can create your war (Export/WAR file right clicking on the project in the project explorer), and then, through your zip application, go to that WEB-INF/lib folder in the war file, and checking that your jar files are there.
If they are not there, Eclipse is not exporting your libraries into the war file correctly. Have you added them to your build path(java build path / Libraries and java build path / Order and export)?
回答3:
Add
<packaging>war</packaging>
in your pom.xml, I experienced this problem when I momentarily change the packing to jar when by using the library via
mvn clean install
which compile it to jar and can be referenced by your other projects, which is not possible to do when your packaging is war. When you forgot to change the packing back to war I see that exact error stacktrace comes up
来源:https://stackoverflow.com/questions/22433604/tomcat-jersey-eclipse-classnotfound-org-glassfish-jersey-servlet-servletcontaine