21

I'm trying to follow this tutorial on creating a simple REST web service, however I get to deploying it on tomcat and it throws an exception:

FAIL - Application at context path /restful could not be started
FAIL - Encountered exception org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/restful]]

I've looked around for a solution and found this question and this one and they make me think that it's a servlet-mapping problem however I'm not sure how to fix it!

Here is my log file:

18/12/2012 9:57:16 AM org.apache.catalina.startup.HostConfig deployWAR
SEVERE: Error deploying web application archive /opt/tomcat7/webapps/restful.war
java.lang.IllegalStateException: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/restful]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

here is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <servlet>
    <servlet-name>RestfulContainer</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mcnz.ws</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>RestfulContainer</servlet-name>
    <url-pattern>/resources/*</url-pattern>
  </servlet-mapping>    

</web-app>

and at the risk of giving you all far too much information, here is a ls of my folder structure:

.:
WEB-INF

./WEB-INF:
classes
lib
web.xml

./WEB-INF/classes:
com

./WEB-INF/classes/com:
mcnz

./WEB-INF/classes/com/mcnz:
ws

./WEB-INF/classes/com/mcnz/ws:
HelloWorldResource.class
HelloWorldResource.java

./WEB-INF/lib:
asm-3.1.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
jersey-client-1.16.jar
jersey-core-1.16.jar
jersey-json-1.16.jar
jersey-server-1.16.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
Community
  • 1
  • 1
Pete
  • 1,095
  • 3
  • 9
  • 17
  • Here's a similar post - http://stackoverflow.com/questions/8512588/tomcat-lifecycleexception-when-deploying – EJK Dec 17 '12 at 23:42
  • 1
    Yes, I am aware as I posted in my question `"I've looked around for a solution and found this question".` I'm just not sure whether it is a) a servlet issue and b) what I need to do to fix it – Pete Dec 17 '12 at 23:55
  • 1
    Sorry, did not follow the link you posted. Anyways, it sounds very much like a problem in web.xml. If I were you, I would try the following... Take the servlet and servlet-mapping elements out of web.xml. Re-deploy and see if you get the same error. If not, then you have a better idea of the root cause. You may also want to verify that com/sun/jersey/spi/container/servlet/ServletContainer.class exists in one of the jar files in your WEB-INF/lib folder (start with jersey-server-1.16.jar). – EJK Dec 18 '12 at 00:17
  • I went through each jar and looked for `com/sun/jersey/spi/container/servlet/ServletContainer.class` and couldn't find it anywhere. So I found the jar file `jersey-servlet.jar` and added that to my lib and it's all working now. Thanks heaps!! – Pete Dec 18 '12 at 02:29

2 Answers2

13

As the above comment shows, the problem turned out to be that the application web.xml referenced a java class in a servlet definition. The problem was corrected by making sure the application actually contained that class. The missing jar file was located and put in the WEB-INF/lib directory.

EJK
  • 12,332
  • 3
  • 38
  • 55
0

I ran into a similar problem with getting ncwms to run on Apache Tomcat/8.5.38 apparently I needed to now add some extra java libraries that were removed in latest java version.

(see details here: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9 )

The change was in this file: /opt/tomcat/bin/catalina.sh

JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS --add-modules java.xml.bind"

But even this wasn't enough for ncwms. It ran into problems importing datasets later, so I reverted to a different version of java:

java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
user3788120
  • 447
  • 4
  • 4