0

My environment: groovy -v

Groovy Version: 3.0.4 JVM: 1.8.0_242 Vendor: Azul Systems, Inc. OS: Linux

I write two files: AStar.groovy and digit.groovy.

file AStar.groovy like:

class State{
    int deep = 0;
    def pre;
    double f = 0;
}

abstract class AStar {
    State begin;
    State end;
    def astar() {
        // some astar code
    }
    abstract def judge(def state);
}

and file digit.groovy like:

class DigitState extends State {
    // some fields and methods
}
class Digit extends AStar {
    // some fields and methods;
    def judge(def state) {
        // some codes.
    }
}
def digit = new Digit();
def path = digit.astar();
path.each {
    println(it);
}

now I want to run digit as: groovy digit.groovy

But it tell me "unable to resolve class State"

I see this url: Including a groovy script in another groovy

But I can not run my script by that way, and I do not know what is my wrong.

How can I include the file without compile please?

xunitc
  • 207
  • 1
  • 2
  • 10

1 Answers1

0

In digit.groovy, use:

import AStar

if they are in the same package.

I tried your code in Eclipse and it ran even without the import statement.

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23
  • Thank you, but it can not run just under terminal. I import Astar and State in digit.groovy, but it still tell me "unable to resolve class AStar". maybe the Eclipse complied them? – xunitc Jun 14 '20 at 07:47
  • You can compile a groovy class with `groovyc` Also take note of canonical package names in your import, I don't know your project structure. – ou_ryperd Jun 14 '20 at 07:50
  • I know it will be ok when I use groovyc to complied the AStar.groovy, but I just want them run as script, without complied. So I think, is there a groovy way to do this, like php's include file, or python's import file? – xunitc Jun 14 '20 at 07:56
  • Put them in the same script. That's the only answer I have for you. – ou_ryperd Jun 14 '20 at 07:59