问题
Hi Guys i am new to maven and webapps, so please bear with me. I have gone through most of the solutions in stack overflow like java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
But still i am unable to resolve this issue. I am using jersey lib version 1.18.1 And i am successfully able to import com.sun.jersey.spi.container.servlet.ServletContainer (tried to make sure its in scope for build path)
I have posted my sample project in git https://github.com/varuntewari/Restful-API Can you please help me to identify the problem?
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5253)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5543)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
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:745)
回答1:
So a couple things. First the problem with the ClassNotFoundExcetion
. The class is in the jersey-servlet
jar. Looking inside the built .war
file all the jars are build to the war. So if you deployed the war manually it should work.
This is a problem I see a lot with Eclipse users. I don't know if you are using Eclipse, what I have come to figure out is that, in the problem cases, Eclipse doesn't deploy the actual war to the server but creates an internal instance of the server and deploys the project artifacts instead of the actual war. In which case it doesn't load the jars.
As a sanity check, you can add this jetty-maven-plugin
. I use it all the time for development
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.6.v20141205</version>
</plugin>
...
</plugins>
Then from the command line, you can run mvn jetty:run
. This will deploy your app to an embedded Jetty instance created by Maven. I tested with your project it and works fine.
The second thing is the dependencies. You need to decide what version of Jersey you want to user, a 1.x
version or a 2.x
version. Which ever you choose, you should get rid of all the other version artifacts.
For your current project, assuming you wan to use Jersey 1.x, you should get rid of these two in your current project.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
</dependency>
Removing those two and running with the Jetty plugin should work fine. As far as trying to run it from the IDE, I am not sure how to solve that problem.
回答2:
So finally i made it work with Jboss. If others are facing this problem and don't want to use jetty (i still use it during development phase). Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies -> Finish. Clean install maven build. Copy war file to Jboss standalone deployment folder.
回答3:
There seemed to be a version issue. Just change the asm version used in the pom from 3.3.1 to 3.1 and it works.
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>
and not
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
Output of code
来源:https://stackoverflow.com/questions/33327036/classnotfoundexception-com-sun-jersey-spi-container-servlet-servletcontainer