Groovy compilation fails with `Unable to load class due to missing dependency`

I am trying (and failing) to compile a Java class which depends on a Groovy class in Gradle 2.4.

Here is the Gradle log:

Parallel execution with configuration on demand is an incubating feature.
:compileJava UP-TO-DATE
startup failed:
General error during conversion: java.lang.NoClassDefFoundError: Unable to load class due to missing dependency uk/co/rx14/jlaunchlib/auth/MinecraftAuthResult
java.lang.RuntimeException: java.lang.NoClassDefFoundError: Unable to load class due to missing dependency uk/co/rx14/jlaunchlib/auth/MinecraftAuthResult
        at org.codehaus.groovy.control.CompilationUnit.convertUncaughtExceptionToCompilationError(
        at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(
        at org.codehaus.groovy.control.CompilationUnit.compile(
        at org.codehaus.groovy.control.CompilationUnit.compile(
        at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(
        at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(
        at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.execute(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(
        at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(
        at org.gradle.messaging.remote.internal.hub.MessageHub$
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(
        at org.gradle.internal.concurrent.StoppableExecutorImpl$
        at java.util.concurrent.ThreadPoolExecutor.runWorker(
        at java.util.concurrent.ThreadPoolExecutor$
Caused by: java.lang.NoClassDefFoundError: Unable to load class due to missing dependency uk/co/rx14/jlaunchlib/auth/MinecraftAuthResult
        at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(
        at org.codehaus.groovy.ast.ClassNode.lazyClassInit(
        at org.codehaus.groovy.ast.ClassNode.getInterfaces(
        at org.codehaus.groovy.ast.ClassNode.getAllInterfaces(
        at org.codehaus.groovy.ast.ClassNode.getAllInterfaces(
        at org.codehaus.groovy.ast.ClassNode.getAllInterfaces(
        at org.codehaus.groovy.control.ResolveVisitor.resolveNestedClass(
        at org.codehaus.groovy.control.ResolveVisitor.resolve(
        at org.codehaus.groovy.control.ResolveVisitor.resolve(
        at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(
        at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(
        at org.codehaus.groovy.control.ResolveVisitor.visitConstructorOrMethod(
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(
        at org.codehaus.groovy.ast.ClassNode.visitContents(
        at org.codehaus.groovy.ast.ClassCodeVisitorSuisitClass(
        at org.codehaus.groovy.control.ResolveVisitor.visitClass(
        at org.codehaus.groovy.control.ResolveVisitor.startResolving(
        at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(
        ... 19 more

1 error

:compileGroovy FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Total time: 1.414 secs

Here is the Java class:


public interface MinecraftAuth {
     * Uses the given credentials to generate a new
     * {@link MinecraftAuthResult}.
     * @param credentialsProvider The provider of the credentials to
     *                            authenticate with.
     * @return the authentication result to use in the minecraft arguments.
     * @throws ForbiddenOperationException when the credentials are invalid.
     * @throws IllegalArgumentException when something is null.
    MinecraftAuthResult auth(CredentialsProvider credentialsProvider);

     * Refreshes a previously-valid {@link MinecraftAuthResult}. If the
     * refreshing fails, it returns an auth result where valid is set to false.
     * @return the {@link MinecraftAuthResult}.
     * @throws IllegalArgumentException when something is null.
    MinecraftAuthResult refresh(MinecraftAuthResult result);

And the Groovy class:


import groovy.transform.Immutable

@Immutable class MinecraftAuthResult {
    String accessToken, clientToken
    Profile selectedProfile
    boolean valid

    String toString() {
        "${getClass().getName()}(accessToken: $accessToken, clientToken: $clientToken, selectedProfile: $selectedProfile, valid: $valid)"

    public static @Immutable class Profile {
        String name, id

This code compiles perfectly fine in IDEA, and a workaround for now is to simply rename the Java class to MinecraftAuth.groovy

Two things:

  1. Make sure that the Java class that references Groovy classes is under src/main/groovy and not under src/main/java - the files in each directory are compiled with the respective compiler as follows:
  • The src/main/java fiels are compiled with the standard Java compiler - it is faster and guarantees that these classes do not depend on Groovy in any way.
  • The src/main/groovy fiels (can mix *.groovy or *.java) are compiled with the Groovy compiler, taking the classes produced by the Java step on the classppath.
  1. If that was not the problem, delete the .gradle directory from the build root and try running a clean build. There was an old bug that was corrupting the Gradle caches if you move a file from Java to Gradle source dirs.

The Java file was always in the src/main/groovy folder, so that’s not the issue. I moved the file back to being a Java class, and it compiled fine. Must have just been a strange state bug that got fixed by renaming it to groovy and back…

I have come across the same error again, this time I have found a fix.

Deleting .gradle/ in the project directory doesn’t fix the problem but gradle clean does so I suppose both .gradle and build/ have to be deleted.