users@jersey.java.net

[Jersey] upgrade 2.5.1 -> 2.7 causing NoClassDefFoundError: Could not initialize class org.jvnet.hk2.internal.Utilities

From: Jack Lista <jackalista_at_gmail.com>
Date: Mon, 7 Apr 2014 10:45:29 -0700

Hi,

We got our services and app working on 2.5.1 (with some difficulty as there
were spring @Autowired integration problems) and now just tried to upgrade
to 2.7 and got the below error. Does anybody know what may be happening
here? It looks like the main problem seems to be with the HK Jersey DI
underpinnings but there's also the WebComponent stuff.

There were also issues with Jackson that meant that we have not used the
jersey-media-json-jackson dependency and instead used this dependency:

        <jackson.version>2.3.0</jackson.version>

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>

Due to the auto-registration feature in the above jacks impl, we likely
have an extra unused jackson module that is not configured nor in use as
it's the one that was auto-registerd. This has not been a problem but may
be now. Has any of that stuff changed significantly? We're trying to
figure out what workaround now needs to be removed, it looks like. Any
assistance would be greatly appreciated.

The log output looks like this:

Apr 07, 2014 9:00:01 AM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: Cannot new
create registration for component type class
org.glassfish.jersey.servlet.WebComponent$WebComponentBinder: Existing
previous registration found for the type.

2014-04-07 09:00:01,204 [http-bio-8080-exec-1] {C: U: P: M:} WARN
til.ErrorTrapServlet: 88 - Error caught. Error map:{http-httpMethod=GET,
javax.servlet.error.status_code=500,
javax.servlet.error.servlet_name=com.ep.resid.ResidualsApplication,
javax.servlet.error.exception_type=class java.lang.NoClassDefFoundError,
stack-trace=java.lang.NoClassDefFoundError: Could not initialize class
org.jvnet.hk2.internal.Utilities
    at
org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl.initialize(ServiceLocatorGeneratorImpl.java:71)
    at
org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl.create(ServiceLocatorGeneratorImpl.java:96)
    at
org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:251)
    at
org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:223)
    at
org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:142)
    at
org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:127)
    at
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:303)
    at
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:284)
    at
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
    at
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:168)
    at
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at
org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:877)
    at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
, javax.servlet.error.exception=java.lang.NoClassDefFoundError: Could not
initialize class org.jvnet.hk2.internal.Utilities,
javax.servlet.error.request_uri



Anyone know what's going on here? We had to monkey around a LOT with
things under the earlier Jersey versions (up to and including 2.5.1) to get
@Autowired to work. Here's our Application configuration, we have no
web.xml except for an error page defn. Our application looks like this:

@ApplicationPath("foo")
public class FooApplication extends ResourceConfig {

    private static final Logger lager =
LoggerFactory.getLogger(FooApplication.class);

    public FooApplication() {

        lager.info("initializing.....");

        // -=j=-: set up an object mapper as we need to configure it for the
        // following:
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        JacksonJsonProvider provider = new JacksonJsonProvider();
        provider.setMapper(mapper);

        // tell Jersey where to find our jax-rs annotated classes
        packages("com.foo.bar").register(provider);

        // Get a reference to the ApplicationContext created by jersey
        ApplicationContext rootCtx =
ContextLoader.getCurrentWebApplicationContext();
        String beans =
Arrays.asList(rootCtx.getBeanDefinitionNames()).toString().replace(',',
'\n');
        lager.info("ROOT CTX:::" + beans + ", beans.length(): " +
beans.length());
    }
}

Our (mostly non-existent) web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<web-app>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error-trap</location>
    </error-page>
</web-app>