2

The following two classes in the same package:

Imported.groovy

class Imported {
  static class Inner {
  }
}

Main.groovy

import Imported

class Main {
  static main(args) {
    new Imported.Inner()
  }
}

When run:

$ groovy Main.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/Main.groovy: 5: unable to resolve class Imported.Inner
 @ line 5, column 5.
       new Imported.Inner()
       ^

1 error

Any reason this is happening? How to properly import static nested classes?

levant pied
  • 3,886
  • 5
  • 37
  • 56

1 Answers1

0

Just compile Imported.groovy: groovyc Imported.groovy so that you have Imported.class and Imported$Inner.class.

Then simply invoke groovy Main.groovy and it should work.

If you want to have some "import/include" functionality, check Including a groovy script in another groovy and how to simply import a groovy file in another groovy script.

Community
  • 1
  • 1
jalopaba
  • 8,039
  • 2
  • 44
  • 57
  • Thanks jalopaba. Why is compilation necessary here? E.g. `new Imported()` works without compiling. Also, this won't be a workable solution when using `groovysh`. – levant pied Nov 20 '14 at 17:08
  • Maybe it is a bug in the compilation process of the script, but I'm not sure. Inner classes have usually been a problem in groovy. – jalopaba Nov 20 '14 at 18:02
  • If the nested class was already compiled? I'm using a 3rd party lib. – ricardogobbo Jun 17 '16 at 12:10