84

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
7
  • Did you run -X for a full debug?
    – farrellmr
    Jun 8, 2017 at 14:50
  • @farrellmr Added debug output to question Jun 8, 2017 at 14:54
  • Same error on maven clean?
    – Thihara
    Jun 8, 2017 at 14:57
  • @Thihara yes, same with mvn clean Jun 8, 2017 at 14:58
  • I dont think Maven 3.3.9 is Java 9 compatible
    – Tome
    Jun 8, 2017 at 15:18

9 Answers 9

42

Problem

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

Sadly I couldn't update to 3.8.3 via the Ubuntu 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 too 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
$ sudo bash -c 'cat << EOF > /etc/profile.d/maven.sh
export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=/opt/maven/bin:$PATH
EOF'

(This assumes that Java is available at the provided path. If this uses the wrong Java, a way to find your current Java path is to run dirname $(dirname $(readlink -f $(which javac))).)

To use it without restarting the shell we make it executable and run it

$ 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.

1
  • Yo, big thumb up for you my man!!!
    – Guerra
    May 23 at 21:12
39

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
4
  • 6
    Considering Java 17 is an LTS version that has been released seven months ago it blows my mind one has to downgrade to 11 to use Maven. But it works for my purpose. Thanks. Apr 21, 2022 at 10:50
  • 1
    This is an ubuntu specific issue, using sdk install maven 3.6.3 makes it go away. (Answer added as well)
    – aksh1618
    Oct 6, 2022 at 11:29
  • 1
    This is the best solution if you don't want break/fix something else. Kudos @aksh1618.
    – HayrolR
    May 2 at 12:51
  • Not just Ubuntu, it also occurs for me on Debian 10 (downloaded openjdk-17.0.2, using system-provided Maven). Jul 2 at 5:44
31

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, e.g...

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)

2
  • 2
    and it does work on java 11 if you cant install java 8 Jun 8, 2021 at 6:09
  • Definitely best maven-specific answer, rather than building from source or changing your system-wide JDK Nov 22 at 8:32
6

This seems to be an ubuntu specific issue because of a custom maven being used. Best solution for me was to switch to sdkman's maven build without having to deal with maven upgradation (my org uses http artifacts, so a lot of workarounds needed with 3.8):

sdk install maven 3.6.3
1
  • Thanks, this was way easier than downloading and setting up maven manually.
    – VishnuVS
    Jul 29 at 14:42
4

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 (e.g. 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
2
  • 1
    If all you need to do in the cli is maven commands, you could use maven toolchains (maven.apache.org/guides/mini/guide-using-toolchains.html) for this purpose as well. Requires one-time configuration, but much more convenient in the longer term.
    – aksh1618
    May 3 at 9:24
  • @aksh1618: Interesting, thanks. I'm happy enough with my solution. If it ever bothers me to prepend java8 or java17, I'll try yours. May 3 at 11:32
2

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

Installing Java 8 on Linux

E.g. 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.

3
  • 2
    Hmm, I'd rather not have to add a ppa Jun 8, 2017 at 15:08
  • 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.
    – Thihara
    Jun 8, 2017 at 15:13
0

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.

0

I had the same issue when trying to use Maven 3.6.3 with Java 17. I've downloaded latest Maven release (3.9.0 at that time) from the official Maven page https://maven.apache.org/download.cgi and unpacked the archive (tar.gz). After modifying the PATH environment variable to contain "bin" subdirectory of the unpacked Maven distribution with export PATH=/home/myuser/maven-3.9.0/bin:$PATH (I'm on Linux) it worked correctly. I could run mvn clean or mvn verify without any issues.

-2

update your mvn

  1. curl -O https://downloads.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
  2. tar -xf apache-maven-3.9.2-bin.tar.gz
  3. sudo mv apache-maven-3.9.2 /opt/maven
  4. export M2_HOME=/opt/maven
  5. export PATH=${M2_HOME}/bin:${PATH}
  6. source ~/.bashrc
  7. mvn --version
  8. mvn clean package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.