0

When I build my Guava dependent project with Gradle using:

//build.gradle

plugins {
    id 'java'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.google.guava', name: 'guava', version: '22.0'
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.3' //Tried 3.1 - 4.0.1 
    distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

I get the following runtime error when running the project in Intellij:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Multiset

The libraries are definitely in the classpath and I cannot figure out why this is happening.

I have solved this problem by switching to an older Gradle version (3.1, 3.2, 3.3 all work).

Therefore, I believe the problem lies with Gradle 3.4+ (3.4, 3.5.1, 4.0 and 4.0.1) all of which I have tried and received the NoClassDefFoundError.

There is no mention of a Gradle version required in the Guava documentation

Is Guava incompatible with newer versions of Gradle?

lmccrate
  • 11
  • 3
  • What Java version are you compiling to? If Java 7 then chances are you need to downgrade guava to version 20.0. – Mateusz Chrzaszcz Jul 24 '17 at 19:11
  • 1
    I am using Java 8. I have also determined that Gradle 3.1, 3.2, and 3.3 work, but Gradle 3.4+ all give me the same NoClassDefFoundError. – lmccrate Jul 24 '17 at 19:17
  • Try again with for instance `gradle clean test --refresh-dependencies` – Mateusz Chrzaszcz Jul 24 '17 at 19:20
  • Tried `gradlew clean test --refresh-dependencies` (I am using a gradle wrapper) and received the same results for <=3.3 working and >3.3 NoClassDefFoundError. – lmccrate Jul 24 '17 at 19:33
  • Could you show me your `build.gradle`? Just edit your question. – Mateusz Chrzaszcz Jul 24 '17 at 20:11
  • 1
    I would guess most likely your classpath for compile time is different than classpath for runtime - would you be able to print your classpath in code and check if guava is there? – Mateusz Chrzaszcz Jul 24 '17 at 20:13
  • You get this exception when doing what? Be precise. – JB Nizet Jul 24 '17 at 20:20
  • @MateuszChrzaszcz You are right! When I print out my classpath at runtime Guava is there when I build with Gradle 3.3 but it disappears with newer versions of Gradle. Any idea why that is happening? – lmccrate Jul 24 '17 at 20:28
  • You get this exception when doing what? Be precise. – JB Nizet Jul 24 '17 at 20:33
  • @MateuszChrzaszcz yep the classpath. I can't figure out what is making Guava disappear with newer Gradle versions. It is still noticed as a dependency and is found at compile time, but missing at runtime. – lmccrate Jul 24 '17 at 20:33
  • @JBNizet Edited the original question to answer yours. Runtime error when running project with Intellij. – lmccrate Jul 24 '17 at 20:35

2 Answers2

0

The reason behind such behavior lies in your classpath. Compile time classpath is different than classpath for runtime. You pass guava to javac but you don't have it available at runtime.

Very good explanation of quite a similar issue is here: NoClassDefFoundError at Runtime with Gradle. I can't reproduce this issue locally. Consider going to your gradle cache and remove everything.

Mateusz Chrzaszcz
  • 1,240
  • 14
  • 32
  • Any idea how this could be happen with changing Gradle versions? – lmccrate Jul 24 '17 at 20:37
  • I see you edited your question, I am trying to figure it out. It's strange, try adding sth like this: `sourceSets.main.runtimeClasspath = sourceSets.main.compileClasspath` It should not be required at all though. – Mateusz Chrzaszcz Jul 24 '17 at 20:45
  • I tried the link and your suggestion and neither have worked. I may post another question because now the parameters are more specific. – lmccrate Jul 25 '17 at 14:52
0

The problem ended up being with the version of Intellij I was using (2016.3.1)

Once I updated Intellij to 2016.3.7 the project runs fine with all versions of Gradle.

lmccrate
  • 11
  • 3