Eclipse : Unable to load jar's into the Tomcat

2019-08-07 06:30发布

问题:

I'm getting this tomcat error " java.lang.ClassNotFoundException:org.glassfish.jersey.servlet.ServletContainer" when I run Tomcat using eclipse :

I see that eclipse does not include the jars into the (tomcat 7.0) WEB-INF/Lib folder, though I tried this :

  1. Included the Maven dependencies in "Web Deployment Assembly" under Project properties. ( Maven downloaded the dependencies in the POM.xml , but these jars are not getting picked up by eclipse )

    The POM.xml as follows:

<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.dcr.javacode.rest.jersey</groupId>
  <artifactId>JAXRS-HelloJerseyExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <repositories>
		<repository>
			<id>maven2-repository.java.net</id>
			<name>Java.net Repository for Maven</name>
			<url>http://download.java.net/maven/2/</url>
			<layout>default</layout>
		</repository>
	</repositories>
  
  <dependencies>
    	<dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.17</version>
        </dependency>        
  </dependencies>
</project>

web.xml as follows :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JAXRS-HelloJerseyExample</display-name>
 	<servlet>
    	<servlet-name>JerseyRESTService</servlet-name>
    	<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    
    	<init-param>
	        <param-name>jersey.config.server.provider.packages</param-name>
	        <param-value>com.dcr.javacode.rest.jersey</param-value>
    	</init-param>
    	
    	<load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
    	<servlet-name>JerseyRESTService</servlet-name>
    	<url-pattern>/rest/*</url-pattern>
  	</servlet-mapping> 	
</web-app>

回答1:

Couple things I see wrong.

  1. You are missing the <packaging> element in the pom file. If this is to be deployed to a servlet container, then the package should be war. The default is jar
  2. You are using jersey-server which doesn't have the ServletContainer. Instead use jersey-container-servlet

But...

The easiest way to get started with Jersey is to use a Maven archetype.

In Eclipse:

  1. [File] → [New] → [Other]
  2. [Maven] → [Maven Project] → Next
  3. Keep defaults (make sure "Create a simple project is unchecked) → Next
  4. In the next dialog, in the "Filter" field, type jersey-quickstart-webapp. It might take a few moments to load all the archetypes. You can see the progress of the archetypes loading in a little progress bar at the very bottom right of the IDE. If you don't see the progress bar and there are no search results, then most likely you don't have the archetypes. In that case, follow the instructions in the first part of this answer
  5. Select the org.glassfish.jersey.archetypes version. It should default to the latest version (at this time it's 2.17) → Next
  6. Type in your groupId, artifactId, and package → Finish

That should get you a running Jersey starter app, that will run on Tomcat