0

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>

4 Answers 4

2

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 ;)

0
1

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

1

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.

-1

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.

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.