Can't Tell
Can't Tell

Reputation: 13416

NoClassDefFoundError when running Jersey in GAE

I am trying to expose a web service using Jersey in GAE.

I am getting the following error:

[INFO] java.lang.NoClassDefFoundError: sun.misc.Unsafe is a restricted class. Please see the Google  App Engine developer's guide for more details.
[INFO]  at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
[INFO]  at org.glassfish.jersey.internal.util.collection.ConcurrentHashMapV8.getUnsafe(ConcurrentHashMapV8.java:3502)
[INFO]  at org.glassfish.jersey.internal.util.collection.ConcurrentHashMapV8.<clinit>(ConcurrentHashMapV8.java:3467)
[INFO]  at org.glassfish.jersey.internal.util.collection.DataStructures.createConcurrentMap(DataStructures.java:237)
[INFO]  at org.glassfish.jersey.message.internal.MessageBodyFactory.<init>(MessageBodyFactory.java:197)

Is this a known issue? If so, is there a workaround?

I am using Maven and used the following dependency to add Jersey to the project

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.3.1</version>
</dependency>

Following is the part from my web.xml where I configured Jersey:

    <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>com.mycompany.myapplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

Upvotes: 0

Views: 1211

Answers (4)

Christian Schmidt
Christian Schmidt

Reputation: 171

I just ran into the same issue: Everything works locally with jersey 2.3.1 and breaks on appengine due to the sun.misc.Unsafe class. I didn't want to go back to older jersey Versions, so I checked the jersey-source to maybe find a patch for this. I found out, that they actually select the ConcurrentMap-implementation based on the flag JdkVersion.IS_UNSAFE_SUPPORTED resulting in either ConcurrentHashMap (when unsupprted) or ConcurrentHashMapV8 (when supported). So I patched the class JdkVersion by removing

static {

  boolean isUnsafeFound;

  try {
    isUnsafeFound = Class.forName("sun.misc.Unsafe") != null;
  } catch (Throwable t) {
    isUnsafeFound = false;
  }

  IS_UNSAFE_SUPPORTED = isUnsafeFound;
}

and setting the flag to

private static final boolean IS_UNSAFE_SUPPORTED = false;

With this my app works fine with jersey 2.3.1 on appengine ;)

Upvotes: 2

Alexis Seigneurin
Alexis Seigneurin

Reputation: 1483

You will need to use an older version of Jersey as version 2 is not compatible with GAE.

Version 1.5 is listed by Google as being compatible: https://code.google.com/p/googleappengine/wiki/WillItPlayInJava

From my experience, Jersey 1.17 works fine.

Upvotes: 1

dendini
dendini

Reputation: 3952

You should add java.misc.Unsafe to the lib folder of the web application so that it is available at runtime.

Alternatively I suggest you to use NetBeans or any other IDE and create a REST service from there, it will automatically configure needed libraries and add them for you to the library folder.

As last, check that you're using the correct JVM when compiling and when deploying.

Upvotes: -1

Dave W. Smith
Dave W. Smith

Reputation: 24956

java.misc.Unsafe is not available in the Java runtime on App Engine.

Upvotes: 1

Related Questions