Check out our Angular Book Series.

Why won't Jersey work on JDK 9?

I've been experimenting with writing REST Services in Java. A ton of clients have used Java to power their backend's over the years, but I never created a REST Service environment from scratch myself. I decided to tackle it during the holiday time and educate myself. I try to focus on front end programming technologies, but knowing about the backend is beneficial.

I decided to use Jersey to build out REST Services in Java and the road has been rocky.

I have a Maven project setup to use Tomcat 8.5 and Java 9.01. Unfortunately, I kept seeing a lot of errors like this in the Java console:


java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
    implementation=org.glassfish.jersey.jaxb.internal.XmlRootObjectJaxbProvider$General
    contracts={javax.ws.rs.ext.MessageBodyReader}
    scope=javax.inject.Singleton
    qualifiers={}
    descriptorType=CLASS
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=null
    proxiable=null
    proxyForSameScope=null
    analysisName=null
    id=130
    locatorId=0
    identityHashCode=1215770148
    reified=false)
    at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:705)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor

etc.. etc..

or this:


java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
    at org.glassfish.hk2.utilities.cache.LRUHybridCache.compute(LRUHybridCache.java:315)
    at org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl.getAllMethods(ClassReflectionHelperImpl.java:108)
    at org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl.getPreDestroyMethod(ClassReflectionHelperImpl.java:209)
    at org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl.access$400(ClassReflectionHelperImpl.java:56)
    at org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl$2.compute(ClassReflectionHelperImpl.java:74)
    at org.glassfish.hk2.utilities.reflection.internal.ClassReflectionHelperImpl$2.compute(ClassReflectionHelperImpl.java:70)
    at org.glassfish.hk2.utilities.cache.LRUHybridCache$OriginThreadAwareFuture$1.call(LRUHybridCache.java:115)
    at org.glassfish.hk2.utilities.cache.LRUHybridCache$OriginThreadAwareFuture$1.call(LRUHybridCache.java:111)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.glassfish.hk2.utilities.cache.LRUHybridCache$OriginThreadAwareFuture.run(LRUHybridCache.java:173)
    at org.glassfish.hk2.utilities.cache.LRUHybridCache.compute(LRUHybridCache.java:292)
    at
etc..etc..

They went on and on. Basically, some libraries that are required by Jersey are no longer included as part of the Java 9 SDK. They were there in Java 8, but were removed. These errors exist because the libraries cannot be found.

The trick to remove these errors is to load these libraries using Maven. Find the pom.xml that builds your project and find the dependencies section. It will probably look something like this:


<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
-->

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
</dependencies>

We need to add back in the missing dependencies. There are two libraries that were required:


<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>

Relaunch your Tomcat instance and you are good to go. No errors and your Jersey services should work without issue.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.