简体   繁体   中英

Maven crashes when trying to compile a project "Error executing Maven."

I'm trying to compile a project that uses maven.

Running mvn compile results in the following error:

[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item
[ERROR] Caused by: Unable to load cache item
[ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$ReflectUtils

This happens anywhere that I run this, it doesn't have to be in the project directory.

What am I doing wrong?

maven 3.3.9, ubuntu 17.04

Full output from mvn -X :

Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 9-Ubuntu, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-openjdk-amd64
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "4.10.0-21-generic", arch: "amd64", family: "unix"
[ERROR] Error executing Maven.
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Unable to load cache item
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2205)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3951)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3955)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4870)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4876)
    at com.google.inject.internal.FailableCache.get(FailableCache.java:48)
    at com.google.inject.internal.ConstructorInjectorStore.get(ConstructorInjectorStore.java:50)
    at com.google.inject.internal.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:137)
    at com.google.inject.internal.InjectorImpl.initializeBinding(InjectorImpl.java:533)
    at com.google.inject.internal.AbstractBindingProcessor$Processor$1.run(AbstractBindingProcessor.java:160)
    at com.google.inject.internal.ProcessedBindingData.initializeBindings(ProcessedBindingData.java:44)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:123)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
    at com.google.inject.Guice.createInjector(Guice.java:99)
    at com.google.inject.Guice.createInjector(Guice.java:73)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at org.codehaus.plexus.DefaultPlexusContainer.addPlexusInjector(DefaultPlexusContainer.java:481)
    at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:206)
    at org.apache.maven.cli.MavenCli.container(MavenCli.java:545)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:281)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:547)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.IllegalStateException: Unable to load cache item
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:79)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.get(LoadingCache.java:34)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.create(AbstractClassGenerator.java:291)
    at com.google.inject.internal.cglib.reflect.$FastClass$Generator.create(FastClass.java:65)
    at com.google.inject.internal.BytecodeGen.newFastClass(BytecodeGen.java:204)
    at com.google.inject.internal.DefaultConstructionProxyFactory.create(DefaultConstructionProxyFactory.java:55)
    at com.google.inject.internal.ProxyFactory.create(ProxyFactory.java:159)
    at com.google.inject.internal.ConstructorInjectorStore.createConstructor(ConstructorInjectorStore.java:90)
    at com.google.inject.internal.ConstructorInjectorStore.access$000(ConstructorInjectorStore.java:29)
    at com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:37)
    at com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:33)
    at com.google.inject.internal.FailableCache$1.load(FailableCache.java:37)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3540)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2321)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2284)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2199)
    ... 28 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.google.inject.internal.cglib.core.$ReflectUtils
    at com.google.inject.internal.cglib.reflect.$FastClassEmitter.<init>(FastClassEmitter.java:67)
    at com.google.inject.internal.cglib.reflect.$FastClass$Generator.generateClass(FastClass.java:77)
    at com.google.inject.internal.cglib.core.$DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache$2.call(LoadingCache.java:54)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:61)
    ... 44 more

Problem

I faced the same issue. It was due to maven 3.6.3 being incompatible with jdk17.

Sadly I just could update to 3.8.3 via the package manager so I installed it manually.
Steps to install on ubuntu are below:

Solution

Requirements

Maven 3.3+ requires JDK 1.7 or above to be installed.
(Might be obsolete, hence you encountered this problem because you used a to novel JDK)

Step-by-step

Download Apache Maven. I installed 3.8.4, you can check for the newest version here: https://maven.apache.org/download.cgi

We save the archive to /tmp

$ TMP_MAVEN_VERSION=3.8.4
$ wget https://apache.org/dist/maven/maven-3/$TMP_MAVEN_VERSION/binaries/apache-maven-$TMP_MAVEN_VERSION-bin.tar.gz -P /tmp

Then unzip it to /opt and remove the archive

$ sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
$ sudo rm /tmp/apache-maven-*-bin.tar.gz

Next we create a sym-link

$ sudo ln -s /opt/apache-maven-$TMP_MAVEN_VERSION /opt/maven

Lastly we set up the env

$ sudo touch /etc/profile.d/maven.sh
$ sudo chown ubuntu /etc/profile.d/maven.sh
$ > /etc/profile.d/maven.sh echo "export JAVA_HOME=/usr/lib/jvm/default-java"
$ >> /etc/profile.d/maven.sh echo "export M2_HOME=/opt/maven"
$ >> /etc/profile.d/maven.sh echo "export MAVEN_HOME=/opt/maven"
$ >> /etc/profile.d/maven.sh echo "export PATH=$M2_HOME/bin:\$PATH"

To use it without restarting we make it executable and run in

$ sudo chmod +x /etc/profile.d/maven.sh
$ source /etc/profile.d/maven.sh

Now you can verify that it is properly installed

$ mvn -v
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /opt/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.11.0-1022-aws", arch: "amd64", family: "unix"

All in one solution

For convenience I added a public Gist with a bash script which prepares your system, you either reboot or run one additional command to be able to use it:

source /etc/profile.d/maven.sh

https://gist.github.com/Illutax/683483a4f50a749ea77bff0cb9637dea
This script takes one optional argument to install an alternative version.
Run it like that: sh maven-install.sh 3.8.4 && source /etc/profile.d/maven.sh and you're good to go

Source

Derived by a post on linuxize.com .

Looks like that version of maven doesn't work on Java 9, but it does on Java 8.

Ensure that you have the relevant packages installed, eg..

sudo apt install openjdk-8-jre openjdk-8-jdk

And then make sure that maven uses the correct version. The simplest way to do this is to add the following line to the end of ~/.mavenrc (Creating it if needed):

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

(You might need to look around in /usr/lib/jvm/ for the exact name you need as it might be different for your system)

I have to juggle with Java8 and Java17 projects, and don't want to use update-alternatives every time I want to run a jar or maven.

I added those 2 aliases to my start scripts (eg to .bashrc or .zshrc ):

alias java8='JAVA_HOME=/usr/lib/jvm/bellsoft-java8-full-amd64 PATH=/usr/lib/jvm/bellsoft-java8-full-amd64/bin/:$PATH'
alias java17='JAVA_HOME=/usr/lib/jvm/bellsoft-java17-full-amd64 PATH=/usr/lib/jvm/bellsoft-java17-full-amd64/bin/:$PATH'

Feel free to adapt them to your specific JAVA versions.

Now, if I want to run maven for a Java8 project, I simply prepend java8 :

❯ mvn clean package

[ERROR] Error executing Maven.
[ERROR] java.lang.IllegalStateException: Unable to load cache item
[ERROR] Caused by: Unable to load cache item
[ERROR] Caused by: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper


❯ java8 mvn clean package

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 

Same goes for JARs:

java8 java -jar some_old.jar
java17 java -jar some_new.jar

Looks like you are using Java 9. Try switching to a stabler version of Java 8.

Installing Java 8 on Linux

Eg using this tutorial How To Install Java with Apt-Get on Ubuntu 16.04

That's the easy option, you can always download one directly. But before bemoaning about such semantics do try the easy way to check if the solution works :) If it does work you can always do a complete uninstall, remove the ppa and get to work on the semantics.

Make sure your guice and java sdk are compatible. I had the same issue when I had guice 4.2.2 with jdk 16. The problem was solved after I upgraded guice to 5.0.1.

If you are using Java 16 this may be because of that as well. Try using ubuntu 20.x server if java 16 is a must for your use case.

I had the same problem in Ubuntu/Mint 20.04 when using OpenJDK 17. I found out that with OpenJDK 11 works well.

Key elements to consider:

# Have version 11 installed
sudo apt install openjdk-11-jdk
# Update alternatives to use version 11 as default
sudo update-alternatives --config java
# Set the JAVA_HOME environment variable accordingly
export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM