【问题标题】:GroovyShell - Error when splitting a script in two parts (MissingMethodExceptionNoStack)GroovyShell - 将脚本分成两部分时出错 (MissingMethodExceptionNoStack)
【发布时间】:2015-11-17 23:37:51
【问题描述】:

我对 Groovy 完全陌生,所以我希望答案不是很明显......

假设我有一个脚本“Test.groovy”:

class A {
    def greet() {println "Hey there!"}
}

new A().greet()

我用GroovyShell(来自Java)评估这个脚本:

new GroovyShell().evaluate(new File("Test.groovy"));

我得到了预期的输出:

你好!

现在,我从脚本中删除最后一行,而是在单独调用 evaluate() 时对其进行评估,但我得到了一个非常模糊的异常。

“Test.groovy”:

class A {
    def greet() {println "Hey there!"}
}

Java:

GroovyShell shell = new GroovyShell();
shell.evaluate(new File("Test.groovy"));
shell.evaluate("new A().greet()");

org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:没有方法签名:A.main() 适用于参数类型:([Ljava.lang.String;) 值:[[]] 可能的解决方案:wait()、wait(long)、any()、find()、wait(long, int)、each(groovy.lang.Closure)

更有趣的是,如果我让脚本保持原样并只更改 Java 部分,它会完美运行(我得到两个“嘿!”)

【问题讨论】:

    标签: java groovy groovyshell


    【解决方案1】:

    这应该有助于解释您所看到的:http://www.groovy-lang.org/structure.html#_script_class

    Groovy 将您的第一个 .groovy 文件视为脚本,因为最后一行存在于类声明之外。 Groovy 编译成 Java 字节码,而 Java 要求所有代码都定义在一个类中。为了遵守,Groovy 做了一些魔术,并使用 main 方法将您的脚本动态转换为 Java 类——类似于以下内容:

    public class script1440427072752 extends groovy.lang.Script { 
    
        public script1440427072752() {
        }
    
        public script1440427072752(groovy.lang.Binding context) {
            super(context)
        }
    
        public static void main(java.lang.String[] args) {
            org.codehaus.groovy.runtime.InvokerHelper.runScript(script1440427072752, args)
        }
    
        public java.lang.Object run() {
            new A().greet()
        }
    
    }
    public class A extends java.lang.Object { 
    
        public java.lang.Object greet() {
            this.println('Hey there!')
        }
    
    }
    

    当您删除该行时,Groovy 将您的 .groovy 文件视为名为 A 的典型 Java 类。无需动态转换为groovy.lang.Script

    当您尝试执行 A 时,GroovyShell 寻找 main 方法,但找不到,并抛出该错误。

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多