我真的搞清楚腳本Java的支持,我現在有這個測試類的Java ScriptingManager訪問和使用功能
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");
當我嘗試運行這個我得到
run: Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "makeObject" is not defined. (#1) in at line number 1 at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) at main.main(main.java:34) Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "makeObject" is not defined. (#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) at sun.org.mozilla.javascript.internal.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2219) at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1510) at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849) at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162) at 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) at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3161) at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173) at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159) at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214) ... 2 more Java Result: 1 BUILD SUCCESSFUL (total time: 3 seconds)
如何解決它,所以我可以在腳本中使用makeObject函數?
你閱讀文件? http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/ – tom