java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

后端 未结 10 1424
名媛妹妹
名媛妹妹 2020-12-14 20:58

You may feel this is a duplicated question, but none of the questions with the same title solve my problems. I am using Jersey 2.0 creating a RESTful web service in Eclipse,

相关标签:
10条回答
  • 2020-12-14 21:39

    Message is simple enough to identify the root cause the Jersey libraries are not in classpath.

    The first thing you should check that weather do you have the dependencies for jersey defined in you pom.xml or not. if not here are the dependencies you can define. if it is already defined in pom.xml, ignore adding dependencies in your pom.xml.

         <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.19</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.19</version>
        </dependency>

    Solution – Add Jersey Library in Deployment Assembly

    1. Right-click on your project in Project Explorer

    2. Open your project’s deployment assembly configuration.

    3. Add Build path jar files in assembly so that they can be added to lib folder in final war file.

    4. Updated assembly will look like this.

    Click ok. Now you are good to go.

    0 讨论(0)
  • 2020-12-14 21:44

    I am also observing the same issue. I am using tomcat8 with Maven. If I build Dynamic Web Project and add all libraries manually, project works fine but if I use Maven, it gives following error:

    SEVERE: Servlet [Jersey REST Service] in web application [/EmployeeManagement] threw load() exception
    java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1313)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1164)
        at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
        at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
        at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
        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:744)
    

    I have added following maven dependencies in pom.xml file:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.21</version>
    </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.21</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-14 21:47

    I got this problem with Eclipse. My project which has a very similar setup - Jersey 2, Tomcat 7. I'm running tomcat from inside eclipse, and it seems that somewhere along the way eclipse sort of forgot how to deploy the libraries to tomcat properly.

    I tried deploying my war file manually to tomcat and starting it via startup.sh and it worked fine, but if I start it up via eclipse I get my web content but the Jersey classes aren't there so the REST API doesn't work. My login servlet works fine, just not the jersey bit. The console has the same error:

    java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    

    on startup. So it must be a problem with how eclipse deploys code to tomcat. I tried going into eclipse servers and clearing the work dir, didn't do anything. (not that it would, for missing classes).

    The only thing that worked was going to my Servers config in Eclipse and deleting my tomcat entry then creating a new one. I did that and it fixed it straight away.

    0 讨论(0)
  • 2020-12-14 21:47

    Putting the jersey jar files in WEB-INF/lib should work.

    If you're not able to put your jersey jar files in WEB-INF/lib, try copy pasting it manually by going through your workspace and restart eclipse ! That worked for me.

    0 讨论(0)
  • 2020-12-14 21:47

    I would like to add, I had the same problem and @Edin's answer worked, however I was using IntelliJ. So I thought I would add a way to do this for Intellij (for future people like me):

    1. File
    2. Project Structure
    3. Artifacts
    4. You should have an Artifact there named after your project, select it
    5. On the right, you will see a list Available Elements
    6. Select all the available elements, right click, then select the option that adds them to the WEB-INF/Lib
    0 讨论(0)
  • 2020-12-14 21:50

    While all of the above could apply (Eclipse is not the most credible application) make sure that you have double checked your maven dependencies. You should note that for jersey 2.19 I had to add these 2 maven dependencies:

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.19</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.19</version>
        </dependency>
    
    0 讨论(0)
提交回复
热议问题