0

I have three groovy file: /my.groovy, /my_dir/util.groovy, /my_dir/base.groovy

my.groovy:

def shell = new GroovyShell()
def util = shell.parse(new File("my_dir/util.groovy"))
println(util.run());

util.groovy:

def getName(String name) {
    def base = new base();
    return name * base.getTimes();
}

println(getName('hi,'));

base.groovy:

def getTimes() {
    return 20;
}

Now I run groovy my.groovy, and it can not work because unable to resolve class base. If these files all in the same dir, it can work. How to do it in this case please? (with no compile)

1 Answer 1

0

A.groovy

def b=new B()
println b.greet("world")

./mydir/B.groovy

def greet(n){
    return "hello $n"
}

assume you are in the directory where A.groovy located

how to run:

groovy -cp ./mydir/ A.groovy

in this case you are running class A and specifying to lookup other classes from directory ./mydir/


or you can use packages:

A.groovy

import mydir.*

def b=new B()
println b.greet("world")

./mydir/B.groovy

package mydir

def greet(n){
    return "hello $n"
}

how to run:

groovy -cp . A.groovy
1
  • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?
    – xunitc
    Nov 15, 2018 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.