Java 调用了 groovy 脚本(带求值)和 return 错误
Java called groovy script (with evaluate) and return error
我花了很多时间寻找答案。我找到了类似于 here 的解决方案,但它是错误的。这对我不起作用。
情况:
我有 2 个 groovy 脚本和 java 应用程序。
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 调用了 "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());
如果 Another.groovy 和 test.groovy 在同一个文件夹中,它会完美运行。但是,如果 Another.groovy 在另一个文件夹中,则它不起作用。 Java 返回错误:
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
^
所以我有问题:
- 可以提供一些建议吗?
- 是否可以(Class 从一个脚本扩展 class 从另一个脚本在某处)?
- 还有其他方法吗?
PS。对不起英语不好。
问题已通过导入解决
解决方案:
- 将 GroovyScriptEngine class 更改为 GroovyShell class
- 使用 CompilerConfiguration class 和 setClassPath("root folder")
- 添加包并导入脚本
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代码:
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());
我花了很多时间寻找答案。我找到了类似于 here 的解决方案,但它是错误的。这对我不起作用。
情况:
我有 2 个 groovy 脚本和 java 应用程序。
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 调用了 "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());
如果 Another.groovy 和 test.groovy 在同一个文件夹中,它会完美运行。但是,如果 Another.groovy 在另一个文件夹中,则它不起作用。 Java 返回错误:
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
^
所以我有问题:
- 可以提供一些建议吗?
- 是否可以(Class 从一个脚本扩展 class 从另一个脚本在某处)?
- 还有其他方法吗?
PS。对不起英语不好。
问题已通过导入解决
解决方案:
- 将 GroovyScriptEngine class 更改为 GroovyShell class
- 使用 CompilerConfiguration class 和 setClassPath("root folder")
- 添加包并导入脚本
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代码:
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());