升级Java 9后创建Tomcat及Jersey项目遇报错问题求助
Alright, let's break down what's going wrong here and how to fix it.
Why This Happens
That ZipException leading to an ExecutionException is rooted in incompatibility between old ASM/Jersey versions and Java 9+'s bytecode & module system:
- ASM 3.3.1 is a super old release (2012) — it has zero support for Java 9's new class file format and module structure. When it tries to parse Java 9-compiled classes, it chokes, throwing what looks like a zip error (since class files are technically zip-compatible).
- Jersey 1.19 is part of the legacy 1.x branch, which was never updated to work with Java 9 or later. It relies on that outdated ASM version by default, and clashes with Java 9's stricter class loading rules.
Solutions
Option 1: Upgrade to a Java 9-compatible Jersey Version (Highly Recommended)
Ditch Jersey 1.x entirely and move to Jersey 2.x — versions 2.29.1 and above are built to support Java 9+. You won't even need to manually add ASM, since Jersey 2.x manages a compatible version under the hood.
Update your Maven dependencies to this:
<dependency> <groupId>org.glassfish.jersey.bundles</groupId> <artifactId>jersey-bundle</artifactId> <version>2.39.1</version> </dependency>
Heads up: Jersey 2.x switched its groupId from
com.sun.jerseytoorg.glassfish.jersey. Most of the JAX-RS core APIs (likejavax.ws.rs) will still work, but you'll need to update any Jersey-specific imports fromcom.sun.jerseytoorg.glassfish.jersey.
Option 2: Force a Newer ASM Version (Temporary Workaround)
If you can't upgrade Jersey right now, you can override its outdated ASM dependency with a version that supports Java 9 (ASM 6.0+; 9.x is best):
<dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>9.5</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-bundle</artifactId> <version>1.19</version> <exclusions> <exclusion> <groupId>asm</groupId> <artifactId>asm</artifactId> </exclusion> </exclusions> </dependency>
Warning: This only fixes the bytecode parsing error. Jersey 1.x will still run into other issues on Java 9+ (like restricted reflection access), so this is just a short-term band-aid.
Option 3: Add Java 9 Module Permissions (For Option 2)
If you go with the workaround, you'll need to add these JVM arguments to your Tomcat startup to fix reflection access issues:
--add-modules java.se.ee --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED
These flags open up the restricted packages Jersey 1.x needs to access via reflection.
How to Verify
- Run
mvn clean installto rebuild your project with the updated dependencies. - Restart Tomcat and check if the
ZipExceptionerror is gone.
内容的提问来源于stack exchange,提问作者Carlos López Marín

