Migrate Jersey project to use Java 10 results in java.lang.IllegalArgumentException at jersey.repackaged.org.objectweb.asm.ClassReader.

tl;dr

To use Java 10, switch to Jersey 2.27 (which is the latest as of this date (10/5/18)).


java.lang.IllegalArgumentException
at jersey.repackaged.org.objectweb.asm.ClassReader.<init>

Jersey repackages asm and puts these class files into the jersey-server jar. Just digging through the jar in my IDE, I looked at the ClassReader constructor (that’s what <init> means) to see where IllegalArgumentException is thrown. Note, I’m currently looking at Jersey version 2.25.1

public ClassReader(byte[] b, int off, int len) {
    this.b = b;
    if (this.readShort(off + 6) > 53) {
        throw new IllegalArgumentException();
    }

The 53 is the class file version. According to that link, 53 is for Java 9. So it appears Jersey 2.25.1 only supports up to Java 9. So I changed the Jersey dependency version to 2.26, and looked at the ClassReader constructor and it was exactly the same. So I switched to Jersey 2.27, and this is what I saw

public ClassReader(final byte[] b, final int off, final int len) {
    this.b = b;
    // checks the class version
    // added "support" for java 11 compiled classes
    if (readShort(off + 6) > Opcodes.V11) {
        throw new IllegalArgumentException();
    }

Notice the Opcodes.V11 (which I assume is Java 11). So it looks like for Java 10+ support, you must switch to Jersey 2.27, as also mentioned in this related issue


Update

After seeing your pom, you need to make sure you are changing the Jersey version on all your Jersey dependencies. And also you have so many unnecessary dependencies. The following are all you need. Remove anything else that has Jersey in the name.

KEEP THESE

Change to 2.27

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.27</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.27</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.27</version>
</dependency>

REMOVE THESE

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.25</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.25</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.25</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json</artifactId>
    <version>2.0-m05</version>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
    <version>1.1.1</version>
</dependency>

ADD EXCLUSION

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.5.13</version>
    <exclusions>
        <exclusion>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Asides

  • See the Jersey Migration Guide for some other issues you may face when migrating to version 2.27

    The one for sure thing you will need if you are migrating from pre-2.26 is the jersey-hk2 dependency. See this post for why.

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
    </dependency>
    

Leave a Comment