Thomas W.
Thomas W.

Reputation: 814

IntelliJ build mixed Groovy and Java project fails with cannot find symbol

I have a mixed (Java & Groovy) IntelliJ project. My Groovy class is using an @Builder annotation. I can access the builder from Java but it fails during the build process of the project with the failure...

Error:(27, 105) java: cannot find symbol
symbol:   method builder()
location: class foo.bar.Sample

If I comment out the use of the builder, the project builds perfectly fine and I can see the builder in the resulting class file.

I think the problem is the build order, that javac tries to compile my Java code before the Groovy code is compiled.

Any idea how to fix this?

The IntelliJ version is Community 2017.1.2

* Update *

I try to be more verbose about my project...it is set up like this...

The source roots are:

Project source roots

The DataObject groovy class:

import groovy.transform.builder.Builder

@Builder
class DataObject {
    String message 
}

And the MainClass...

public class MainClass {

    public static void main(String... args) throws Exception {
        System.out.println(DataObject.builder().message("HelloWorld!").build().getMessage());

        DataObject dataObject = new DataObject();
        dataObject.setMessage("HelloWorld!");
        System.out.println(dataObject.getMessage());
    }
}

Groovy-Eclipse is set as the Java Compiler (groovy-eclipse-batch-2.4.3-01.jar) in the bytecode version 1.8

If I try to compile the the project I got the following Error...Build error

I get a similar error (java:cannot find symbel for the builder) if I try it with javac as the Java compiler.

If I remove the use of the builder from Java class, the project builds perfectly and I am able to use the builder, at least as long I don't have any changes on the groovy class.

Upvotes: 4

Views: 3038

Answers (1)

Dany
Dany

Reputation: 1530

You are affected by https://issues.apache.org/jira/browse/GROOVY-3683.

Assuming you've installed Groovy with sdkman.

$ sdk use groovy 2.4.9
Using groovy version 2.4.9 in this shell.

# This is the error you get using joint compilation
$ groovyc -j src/main/groovy/* 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Compile error during compilation with javac.
/Users/danyjb/Downloads/GroovyTest/src/main/groovy/MainClass.java:4: error: cannot find symbol
        System.out.println(DataObject.builder().message("HelloWorld!").build().getMessage());
                                     ^
  symbol:   method builder()
  location: class DataObject
1 error

To resolve the problem you need to compile Groovy and Java separately:

$ groovyc src/main/groovy/DataObject.groovy -d out/
$ javac -cp $GROOVY_HOME/lib/groovy-2.4.9.jar:out/ -d out/ src/main/groovy/MainClass.java

In terms of IntelliJ IDEA this means you need to put your Groovy code in the separate module and add a dependency from the Java module to the Groovy module.

Upvotes: 2

Related Questions