how to simply import a groovy file in another groovy script

前端 未结 1 465
情话喂你
情话喂你 2021-01-13 08:42
~/groovy
 % tree
.
├── lib
│   ├── GTemplate.class
│   └── GTemplate.groovy
└── Simple.groovy


class GTemplate {
  static def toHtml() {
    this.newInstance().toHt         


        
相关标签:
1条回答
  • 2021-01-13 09:20

    It looks like you are confusing Groovy with PHP-like techniques.

    Because it's closer to Java, if a class exists within a subfolder, it needs to exist within a package of the same name. In your example, you could add this line to the top of GTemplate.groovy and recompile the file:

    package lib
    

    However, this means that the fully-qualified name for GTemplate is now actually lib.GTemplate. This may not be what you want.

    Alternatively, if you want to use the files from a subfolder without using packages, you could remove the import statement from Simple.groovy, and instead compile and run the class like so:

    groovyc -classpath $CLASSPATH:./lib/ Simple.groovy
    groovy -classpath $CLASSPATH:./lib/ Simple
    

    NOTE: If you don't have a CLASSPATH already set, you can simply use:

    groovyc -classpath ./lib/ Simple.groovy
    groovy -classpath ./lib/ Simple
    

    Also, for windows machines, change $CLASSPATH: to %CLASSPATH%;

    I strongly recommend learning about packages and understanding how they work. Look at this Wikipedia article on Java packages for a starting point.

    0 讨论(0)
提交回复
热议问题