Java ScriptingManager访问和使用函数

时间:2013-10-27 19:53:47

标签: java javascript scripting

我正在弄清楚Java的脚本支持,我目前有这个测试类

import java.io.FileNotFoundException;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;



/**
 *
 * @author Makerimages
*/
public class main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException {

    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();
    // create JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    // evaluate JavaScript code from given file - specified by first argument
    engine.eval(new java.io.FileReader("C:/Users/**/Documents/NetBeansProjects/test/src/l.js"));

}
public void makeObject(String s)
{
    System.out.println(s);
}

}

和这个l.js文件

makeObject("afzsdf");

当我尝试运行时,我得到了

  

运行:   线程“main”中的异常javax.script.ScriptException:sun.org.mozilla.javascript.internal.EcmaError:ReferenceError:未定义“makeObject”。 (#1)在第1行       at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224)       在javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)       在main.main(main.java:34)   引起:sun.org.mozilla.javascript.internal.EcmaError:ReferenceError:未定义“makeObject”。 (#1)       at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3771)       at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3749)       at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3834)       在sun.org.mozilla.javascript.internal.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2219)       在sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1510)       at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849)       在sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162)       在sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429)       at com.sun.script.javascript.RhinoScriptEngine $ 1.superDoTopCall(RhinoScriptEngine.java:116)       at com.sun.script.javascript.RhinoScriptEngine $ 1.doTopCall(RhinoScriptEngine.java:109)       在sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3161)       在sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173)       在sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159)       at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214)       ......还有2个   Java结果:1   建立成功(总时间:3秒)

我如何修复它以便我可以在脚本中使用makeObject函数???

1 个答案:

答案 0 :(得分:0)

您需要添加绑定,例如将Java对象链接到JS作用域。

爪哇:

import javax.script.*;
import java.io.FileNotFoundException;

public class main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException {

        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");

        //bind main instance
        Bindings bindings = engine.createBindings();
        bindings.put("main", new main());
        engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);

        // evaluate JavaScript code from given file - specified by first argument
        engine.eval(new java.io.FileReader("l.js"));

    }

    public void makeObject(String s) {
        System.out.println(s);
    }
}
在JS中,你引用了这个main对象:

main.makeObject("afzsdf");