Maven 3.6.3与Java 16.0.2兼容异常求助:NoSuchMethodError及Guice替换后问题
Fixing Maven + Java 16 Compatibility Errors (Guice/Guava Issues)
Let's tackle this problem step by step—this is a classic compatibility issue between older Maven versions, Guice/Guava dependencies, and Java 16. Here are two reliable solutions to fix it:
Solution 1: Upgrade Maven to a Java 16-Compatible Version
Maven 3.6.3 is relatively old and lacks full support for Java 16. Upgrading to Maven 3.8.6 or later (the latest stable release as of now is 3.9.x) will automatically resolve the underlying dependency conflicts.
Follow these steps for Ubuntu 20.04:
- Download a compatible Maven binary (we'll use 3.8.6 as an example):
wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz - Extract the archive to
/usr/local:sudo tar xf apache-maven-3.8.6-bin.tar.gz -C /usr/local - Create a symbolic link for easy access:
sudo ln -s /usr/local/apache-maven-3.8.6 /usr/local/maven - Update your environment variables to use the new Maven. Edit your
~/.bashrcfile:
Add these lines at the end:nano ~/.bashrcexport MAVEN_HOME=/usr/local/maven export PATH=$MAVEN_HOME/bin:$PATH - Apply the changes immediately:
source ~/.bashrc - Verify the installation:
You should see Maven 3.8.6+ paired with Java 16.0.2. Now try runningmvn -vmvn packageagain—the error should be resolved.
Solution 2: Replace Incompatible Guice/Guava Dependencies (Keep Maven 3.6.3)
If you can't upgrade Maven right now, you need to replace the outdated Guice and Guava jars in your system Maven's lib directory with versions compatible with Java 16.
Here's how:
- First, backup the old jars to avoid losing them:
sudo mv /usr/share/maven/lib/guice.jar /usr/share/maven/lib/guice.jar.bak sudo mv /usr/share/maven/lib/guava.jar /usr/share/maven/lib/guava.jar.bak - Download compatible versions:
# Download Guice 4.2.3 (no_aop variant, avoids Java 16 module system conflicts) sudo wget -O /usr/share/maven/lib/guice.jar https://repo1.maven.org/maven2/com/google/inject/guice/4.2.3/guice-4.2.3-no_aop.jar # Download Guava 30.1.1-jre (contains the missing checkArgument method overload) sudo wget -O /usr/share/maven/lib/guava.jar https://repo1.maven.org/maven2/com/google/guava/guava/30.1.1-jre/guava-30.1.1-jre.jar - Now test running
mvn package—this should fix both the initial Guice cache error and the subsequentNoSuchMethodError.
Why This Works
- The original
Could not initialize class com.google.inject.internal.cglib.core.$MethodWrappererror happens because older Guice versions use CGLIB, which has compatibility issues with Java 16's module system. Theno_aopGuice variant skips this problematic dependency. - The
NoSuchMethodErroroccurs because your system's Guava version is too old—Guava 30.1.1-jre includes the exact overload ofPreconditions.checkArgumentthat Maven's dependencies rely on.
内容的提问来源于stack exchange,提问作者Gael Marcadet
相关产品推荐
相关产品推荐

