1

i'm working with eclipse creating a dynamic web project. i'm using tomcat 7 and i've included the jar from jersey, here are the jars:

aopalliance-repackaged-2.4.0-b31.jar
asm-debug-all-5.0.4.jar
hk2-api-2.4.0-b31.jar
hk2-locator-2.4.0-b31.jar
hk2-utils-2.4.0-b31.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.4.0-b31.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-guava-2.21.jar
jersey-media-jaxb.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar

my xml is:

<?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" 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>RESTful Jersey Hello World</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Restful</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.lorenzo.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Restful</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

when running my project the trace is this:

set 21, 2015 12:30:52 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
AVVERTENZA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Restful' did not find a matching property.
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server version:        Apache Tomcat/7.0.64
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server built:          Aug 19 2015 17:18:06 UTC
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server number:         7.0.64.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: OS Name:               Windows XP
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: OS Version:            5.1
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Architecture:          x86
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Java Home:             C:\Programmi\Java\jdk1.8.0_60\jre
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: JVM Version:           1.8.0_60-b27
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: JVM Vendor:            Oracle Corporation
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: CATALINA_BASE:         C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: CATALINA_HOME:         C:\Programmi\Apache Software Foundation\Tomcat 7.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dcatalina.base=C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dcatalina.home=C:\Programmi\Apache Software Foundation\Tomcat 7.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dwtp.deploy=C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Djava.endorsed.dirs=C:\Programmi\Apache Software Foundation\Tomcat 7.0\endorsed
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dfile.encoding=Cp1252
set 21, 2015 12:30:52 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMAZIONI: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Programmi\Java\jdk1.8.0_60\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/bin/client;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/bin;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/lib/i386;C:\Programmi\Java\jdk1.8.0_60\bin;C:\oracle\ora92\bin;C:\Programmi\Oracle\jre\1.1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Programmi\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Programmi\Wave Systems Corp\Gemalto\Access Client\v5\;C:\Programmi\Gemplus\GemSafe Libraries\BIN;C:\Programmi\File comuni\Roxio Shared\DLLShared\;C:\Programmi\File comuni\Roxio Shared\9.0\DLLShared\;C:\Programmi\ZipGenius 6\;c:\Programmi\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Serena\Dimensions 14.1\CM\prog;C:\Programmi\Microsoft\Web Platform Installer\;C:\Programmi\Git\cmd;C:\Programmi\Git\mingw32\bin;C:\Programmi\Git\usr\bin;ANT_HOME%\bin;C:\Programmi\nodejs\;C:\Documents and Settings\gcappella\Dati applicazioni\npm;C:\eclipse;;.
set 21, 2015 12:30:52 PM org.apache.coyote.AbstractProtocol init
INFORMAZIONI: Initializing ProtocolHandler ["http-bio-8080"]
set 21, 2015 12:30:52 PM org.apache.coyote.AbstractProtocol init
INFORMAZIONI: Initializing ProtocolHandler ["ajp-bio-8009"]
set 21, 2015 12:30:52 PM org.apache.catalina.startup.Catalina load
INFORMAZIONI: Initialization processed in 835 ms
set 21, 2015 12:30:52 PM org.apache.catalina.core.StandardService startInternal
INFORMAZIONI: Starting service Catalina
set 21, 2015 12:30:52 PM org.apache.catalina.core.StandardEngine startInternal
INFORMAZIONI: Starting Servlet Engine: Apache Tomcat/7.0.64
set 21, 2015 12:30:53 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFORMAZIONI: validateJarFile(C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Restful\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
set 21, 2015 12:30:54 PM org.apache.catalina.startup.TldConfig execute
INFORMAZIONI: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
set 21, 2015 12:30:55 PM org.apache.coyote.AbstractProtocol start
INFORMAZIONI: Starting ProtocolHandler ["http-bio-8080"]
set 21, 2015 12:30:55 PM org.apache.coyote.AbstractProtocol start
INFORMAZIONI: Starting ProtocolHandler ["ajp-bio-8009"]
set 21, 2015 12:30:55 PM org.apache.catalina.startup.Catalina start
INFORMAZIONI: Server startup in 3587 ms

so now i think my problem must be in the xml...what's wrong with it???

Lorenzo
  • 673
  • 1
  • 11
  • 25

5 Answers5

3

This should be resolved by updating your Java version.

The root cause is Unsupported major.minor version 51.0. This means, the class org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer is complied for Java 7. So using Java 7 or higher will solve your problem.

Amila
  • 5,195
  • 1
  • 27
  • 46
2

This is your problem:

Caused by: java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer : Unsupported major.minor version 51.0 (unable to load class org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer)

You need a higher version JDK.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • must i change to installed jre? i have: jdk 1.8_40 jdk 1.8_60 jre 6 (that one was the previous one) but changing to one of the others jdk the error persist – Lorenzo Sep 21 '15 at 09:07
  • @Lorenzo You need JDK 7 as I think you are using the latest Jersey version 2.17.x. But Using JDK 8 should work. As higher JDKs are downwards compatible i.e. they can act as older JDKs. Just change the execution environment of you project to JDK 7. – Oliver Sep 21 '15 at 09:16
  • i've changed that, if you see my edit the trace after changing the execution environment is different – Lorenzo Sep 21 '15 at 09:24
2

The key to the problem is this line from the logging:

Caused by: java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer : Unsupported major.minor version 51.0 (unable to load class org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer)

It means the JVM cannot read a compiled class file because it was compiled for a newer version of the Virtual Machine than the one you're currently running.

As you can read on Wikipedia, version 51.0 means the class is compiled for Java 7 or up. And indeed, as you can see in the Jersey docs, from Jersey 2.6 up, Java 7 is required.

Your logging also reveals the current Java version being used:

INFO: JVM Version:           1.6.0_45-b06

This is indeed too old. Running Java 7 or higher should solve your problem.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
2

Upto Jersey 2.6 you can run it with Java 6(JDK 1.6).

After version 2.6 + onwards you need (JDK 1.7) Java 7.

For Reference: This is my from project. It is fully functional project please refer below. I am using Jersey 2.6 because my production server has Java 6 so I had to lower it from Jersey 2.17 to 2.6.

I invoke my sample webserive like this

localhost:8080/OrderStatusWebService/rest/test/hello-world 

in my browser

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
          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_2_5.xsd"
          version="2.5">

    <display-name>Order Status REST WebService</display-name>


    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.xxx.orderstatus.config.OrderStatusConfig</param-value>

        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


</web-app>

And this is a sample Webservice class

package com.xxx.orderstatus.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/test")
public class TestWebService 
{

    @GET
    @Path("/hello-world")
    public Response checkAuth(@QueryParam("userId") String  userId) {

        return Response.status(200).entity("Hello").build();
    }


}

And since mine is a Maven project, this is my POM.xml. Please note The property <finalName>OrderStatusWebService</finalName>

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx.orderstatus</groupId>
    <artifactId>OrderStatusWebService</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>OrderStatusWebService Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <jdk.version>1.6</jdk.version>
        <jstl.version>1.2</jstl.version>
        <jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
    </properties>
    <repositories>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
        <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>
        <repository>
            <id>snapshot-repository.java.net</id>
            <name>Java.net Snapshot Repository for Maven</name>
            <url>https://maven.java.net/content/repositories/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <dependencies>

        <!-- My Dependencies START -->

        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- JDBC -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <!-- GSON -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <!-- These jars dont' come by default with the latest Jerser Server Container 
            Jars -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <!-- Apache Commons Codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>


        <!-- SLF4j and Log4j Libs -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>


        <!-- Apache Commons IO -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- Multipart Dependencies -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- Commons DBCP 1.4 Starts -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>



        <!-- My Dependencies ENDS -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>OrderStatusWebService</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Oliver
  • 6,152
  • 2
  • 42
  • 75
  • i've installed previously also version 1.8_40 and 1.8_60 but the error persist changing the default jre on properties – Lorenzo Sep 21 '15 at 09:11
  • @Lorenzo If it is a maven project then search for Execution envronment and change it to 1.7. If it is a normal dynamic web project changing the properties of project should solve the problem. Right click the project go the Project Facets and Changes the Java facets to 1.7. This should work. – Oliver Sep 21 '15 at 09:14
  • @Lorenzo Then you are surely not doing it right. It is very clear that there is JDK version error. Do not set the Java facets to 1.6 or 1.8 set it to **Java 1.7** This should solve your problems. please try again. In the Worst case I would suggest you install JDK 7 and remove all other JDK versions(But this is when you have tried everything and things don't seem to work at all). All the best !! – Oliver Sep 21 '15 at 09:25
  • i go to project->properties->project facets-> change the java version from 1.6 to 1.7 the error is still here.. :( – Lorenzo Sep 21 '15 at 09:28
  • Which Jersey API version are you using? Now I think you should start doing crazy things. If your Jersey version is 2.6 and below uninstall all your JDKs and install JDK 1.6 only. If your Jersey version is 2.7 or above then uninstall all JDKs and install only JDK 7. This is such a minor problem and should have been resolved when you did the above task . – Oliver Sep 21 '15 at 09:36
  • yes the major minor has gone, i've downloaded jersey bundle 2.21 i always get errors.....trying with 1.9.2 same result........ – Lorenzo Sep 21 '15 at 09:42
  • @Lorenzo Read this https://jersey.java.net/documentation/latest/modules-and-dependencies.html#d0e560 – Oliver Sep 21 '15 at 09:46
  • so, i changed the servlet name that turns out to be wrong, from Rest to Restful, now i get to the inside browser of eclipse but 404! :( – Lorenzo Sep 21 '15 at 09:58
  • Added Code samples for reference above. – Oliver Sep 22 '15 at 10:44
0

First of all you shouldn't include below library into your classpath, as they available in tomcat library. javax.servlet-api-3.0.1.jar

Also, Please check for jersey-container-servlet-core.jar, jersey-container-servlet.jar libraries are really required in your case, otherwise prefer to delete those jar for you lib folder of project.

Arvind Chavhan
  • 488
  • 1
  • 6
  • 14
  • as you see in the tutorial i have followed i've included all the library he included...anyway changing the version of jdk from 1.6 to 1.7 and 1.8 i get always errors.... – Lorenzo Sep 21 '15 at 09:34