1

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)
Community
  • 1
  • 1
Nova
  • 241
  • 6
  • 16

3 Answers3

2

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • you are the man ! Jetty fixed the problem. On another note there were no references to glassfish in my project. – Nova Oct 25 '15 at 19:50
  • One more thing jetty doesn't seem to consider myapp Like its running http://localhost:8080/getExample/ctofservice/23 instead of http://localhost:8080/myapp/getExample/ctofservice/23 is there something more to be done for this ? – Nova Oct 25 '15 at 23:42
  • 1
    Has nothing to do with the `artifactId`. In an servlet container what matters is the context path, which is sometimes set in a configuration file. When there is no config file, it is often just set to the war name. It just happens that usually the war name is the same as the `artifactId`. That's why many see the correlation. Anyway, to set the context path in the plugin, see [here](http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#configuring-your-webapp). That is the link to the documentation where you can find other configurations also. Also scroll up for more option – Paul Samsotha Oct 26 '15 at 00:49
  • Perfect ! Added required context. – Nova Oct 26 '15 at 03:03
1

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.

Nova
  • 241
  • 6
  • 16
0

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AKASH KHATRI
  • 69
  • 1
  • 3
  • This doesn't seem to be the issue. In fact my project didn't even need asm. It was just the deployment issue. @peeskillet pointed out exact problem. Thanks for your response. – Nova Oct 25 '15 at 19:53
  • Glad that your issue is resolved. I faced the same issue when I download your project and deployed on tomcat from eclipse. so I went ahead to change the asm version and went fine for me. – AKASH KHATRI Oct 26 '15 at 02:00