【问题标题】:Java called groovy script (with evaluate) and return errorJava 调用 groovy 脚本(带评估)并返回错误
【发布时间】:2018-11-18 07:08:13
【问题描述】:

我花了很多时间寻找答案。我找到了类似here 的解决方案,但它是错误的。这对我不起作用。
情况
我有 2 个 groovy 脚本和 java 应用程序。
Another.groovy(自动测试/来源)

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(自动测试/案例)

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()

名为“test.groovy”的 Java 类

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
   ^

所以我有问题:

  • 可以提供一些建议吗?
  • 有可能吗(一个脚本的类扩展了另一个脚本的类)?
  • 还有其他方法吗?

PS。抱歉英语不好。

【问题讨论】:

    标签: java groovy evaluate


    【解决方案1】:

    通过导入解决了问题
    解决方案

    1. 将 GroovyScriptEngine 类更改为 GroovyShell 类
    2. 使用 CompilerConfiguration 类和 setClassPath("root folder")
    3. 添加包并导入脚本

    另一个.groovy(自动测试/来源)

    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(自动测试/案例)

    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());
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多