tags:

views:

37

answers:

1

I can't get static imports to work in Groovy. Is this supposed to work?

(I'm running Groovy 1.7.4 on Windows)

Constants.groovy:

package foo

class Constants {
    static final PI = 3.14
}

Test.groovy:

package foo
import static foo.Constants.PI

class Test {
    static main(args) {
        println("pi=" + PI)
    }
}

EDIT: Both files are stored in the same directory "foo".

When I try to run this I get:

groovy -cp . foo\Test.groovy
Caught: groovy.lang.MissingPropertyException: No such property: PI for class: foo.Test
        at foo.Test.main(Test.groovy:6)
A: 

This is a problem with groovy's dependency resolution. You can work around it by forcing it to compile Constants.groovy first, like so:

groovyc foo/Constants.groovy
groovy foo/Test.groovy

EDIT: downgrading groovy to version 1.7.3 also solves this.

ataylor
i tested this code... without multiple compiles as you have listed and it runs just fine, I believe the files are just not in the right directory
Aaron Saunders
It's definitely a real problem. It failed for me with the same error as the original poster with groovy 1.7.4 on linux. The error doesn't occur with groovy 1.7.3.
ataylor