8

I have a Tomcat 7 project which works like a charm on my Eclipse-integrated test server, but fails to start on the production server.

It previously worked on production too, but I had to add CORS support, which somehow made it fail.

To add CORS support, I added this to my pom.xml

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-catalina</artifactId>
  <version>8.0.22</version>
  <scope>provided</scope>
</dependency>

and this to my web.xml

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

If I remove these lines again, it works on the production server, but CORS doesn't work.

The server throws a ClassNotFoundException when I try to start the application, which is strange, because the class is clearly there in the .war-file.

The catalina log claims a few .jar-files fails to validate because "jar not loaded". The .jars are all in the .war, so I do not understand why it fails.

Output from the logs:

Catalina log:

May 19, 2015 9:10:29 AM org.apache.catalina.util.LifecycleBase stop
INFO: The stop() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/APIMR]] after stop() had already been called. The second call will be ignored.
May 19, 2015 9:10:29 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/APIMR]
May 19, 2015 9:10:36 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat/webapps/APIMR.war
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-el-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-servlet-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/APIMR] startup failed due to previous errors

localhost log:

May 19, 2015 8:22:11 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@d45f6a5')
May 19, 2015 9:10:05 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    <snip>


May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    <snip>

Any suggestions?

1

3 Answers 3

14

The server will reject jars which already belong to the servers runtime (tomcat-**.jar, servlet*.jar, ...). Try this CORS filter instead: http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter

pom.xml

<dependency>
 <groupId>com.thetransactioncompany</groupId>
 <artifactId>cors-filter</artifactId>
 <version>2.4</version>
</dependency>

web.xml

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
4
  • This worked, according to my tests. I will accept this answer if it works for those using the application as well
    – Suppen
    May 19, 2015 at 7:52
  • this web.xml you refer to? is it the one present in tomcat/conf/web.xml?
    – i_raqz
    Mar 29, 2016 at 11:31
  • Thats usually the one from your project.
    – Stefan
    Mar 29, 2016 at 11:53
  • I'm facing the same issue but with another filter... Where does it says the server rejects jars? What's the point of providing filters if we can't use it?
    – Rapster
    Sep 5, 2018 at 8:24
3

In my case adding the dependency did not work. It worked when I added two jars in the tomcat lib folder as below:

cors-filter-2.5.jar

java-property-utils-1.10.jar

1
  • when I oerform the above steps I recieve the below error. SEVERE: Servlet [jersey-servlet] in web application [/SummaryReportApp-API] threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer Mar 23, 2021 at 18:52
0

Although being an old post, this was so helpful. I cannot be grateful enough for sharing this .. Man you saved me from many more days of struggle.

Just in case someone else will bump into the same issue, here is what I done:

  1. Added the maven dependency to my project's pom.xml file: http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter/2.6
  2. Added the filter to my project's web.xml /WEB-INF/web.xml file to read like below and restarted tomcat:
</web-app>
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>packages</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/appapi/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

This seem to be working perfectly with tomcat server, 8 and 9. If the changes wont take effect, restart the IDE "eclipse" in my case. Thank you again for the awesome work guys. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.