从java调用groovy时出错

时间:2015-09-10 06:49:59

标签: java groovy groovyshell

我正在尝试从Java执行一个groovy脚本。这是我想要做的:

public class ScriptExecutor{

    private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

    public CompiledScript compileScript(String language, String script){
        Preconditions.checkNotNull(language, "language parameter cannot be null");
        Preconditions.checkNotNull(script, "script parameter cannot be null");

        try{ 
            ScriptEngine scriptEngine = getScriptEngine(language);
            if (scriptEngine instanceof Compilable) {
                Compilable compilable = (Compilable) scriptEngine;
                return compilable.compile(script);
            }else{
                throw new AppException("Passed script can not be compiled!");
            }
        } catch (ScriptException e){
            LOGGER.error("[UNABLE TO COMPILE SCRIPT: ] ", e);
            throw new AppException("[UNABLE TO COMPILE SCRIPT: ] ", e);
        }
    }

    public static void main(String[] args) {
        ScriptExecutor executor = new ScriptExecutor();
        String script = "def notnull(Object givenValue){String strValue = obj != null ? obj.toString() : null;return strValue == null;}";
        CompiledScript compiledScript = executor.compileScript("groovy", script);
        ScriptEngine engine = compiledScript.getEngine();
        Invocable invocable = (Invocable)engine;
        try {
            invocable.invokeFunction("notnull", "test");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

执行此操作时,我收到以下错误:

java.lang.NoSuchMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.notnull() is applicable for argument types: (java.lang.String) values: [test]
Possible solutions: notify(), notifyAll(), compile(java.lang.String)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeImpl(GroovyScriptEngineImpl.java:396)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeFunction(GroovyScriptEngineImpl.java:192)
    at com.adaequare.processing.script.execution.ScriptExecutor.main(ScriptExecutor.java:188)

我在这里缺少什么东西?请建议。

1 个答案:

答案 0 :(得分:0)

试试这个:

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import groovy.util.ScriptException;



public class ScriptExecutor {

    private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

    public ScriptEngine compileScript(String language, String script) throws javax.script.ScriptException{

        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
        scriptEngine.eval(script);
//      if (scriptEngine instanceof Compilable) {
//          Compilable compilable = (Compilable) scriptEngine;
//          return compilable.compile(script);
//      }else{
//          //throw new AppException("Passed script can not be compiled!");
//      }
        return scriptEngine;
    }

    public static void main(String[] args) throws javax.script.ScriptException {
        ScriptExecutor executor = new ScriptExecutor();
        String script = "def notnull(Object obj){String strValue = obj != null ? obj.toString() : null;return strValue == null;}";
        ScriptEngine engine = executor.compileScript("groovy", script);

        Invocable invocable = (Invocable)engine;
        try {
            Object result=invocable.invokeFunction("notnull", null);
            System.out.print(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}