I spent a lot time finding an answer. I found something like the solution here, but it was wrong. This doesn't work for me.
Situation:
I have 2 groovy scripts and java app.
Another.groovy (autotest/sources)
class Another
{
protected String name="";
public Another() {}
public main(String[] args) {}
public boolean getResult() {return true;}
public String getName() {return name;}
public void setName(String value) {name=value;}
}
test.groovy (autotest/cases)
evaluate(new File("autotest/sources/Another.groovy"))
import support.tool.AutotestResult;
public class Another2 extends Another
{
public Another2()
{
this.setName(this.name+"N");
}
public AutotestResult run()
{
return new AutotestResult(this.name+"123",this.getResult(),null,null)
}
}
Another2 a = new Another2()
a.run()
Java class called "test.groovy"
String[] paths = {"autotest\\cases\\test.groovy"};
GroovyScriptEngine gse = new GroovyScriptEngine(paths);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)gse.run("test.groovy", binding)).toJSON());
It works perfectly if Another.groovy and test.groovy are in the same folder. But if Another.groovy is in another folder it does not work. Java returned error:
Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/.../autotest/cases/test.groovy: 6: unable to resolve class Another
@ line 6, column 1.
public class Another2 extends Another
^
So I have questions:
PS. Sorry for the bad English.
Problem resolved with import
Solution:
Another.groovy (autotest/sources)
package sources
public class Another
{
protected String name="AnotherNama";
public Another() {}
public main(String[] args) {}
public boolean getResult() {return true;}
public String getName() {return name;}
public void setName(String value) {name=value;}
}
test.groovy (autotest/cases)
package cases
import support.tool.AutotestResult;
import sources.Another
public class Another2 extends Another
{
public Another2()
{
this.setName(this.name+"N");
}
public AutotestResult run()
{
return new AutotestResult(this.name+"123",this.getResult(),null,null)
}
}
Another2 a = new Another2()
a.run()
Java code:
CompilerConfiguration config=new CompilerConfiguration();
config.setClasspath("autotest");
config.addCompilationCustomizers(new ImportCustomizer());
GroovyShell shell=new GroovyShell(config);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)shell.run(new File("autotest/cases/test.groovy"),new ArrayList())).toJSON());