Scriptable envGlobals;
InputStreamReader envReader = new InputStreamReader(getClass()
.getResourceAsStream("env.rhino.js"));
// InputStreamReader jqueryReader = new InputStreamReader(getClass()
// .getResourceAsStream("jquery-1.6.2.js"));
try {
Context cx = ContextFactory.getGlobal().enterContext();
try {
Global global = new Global();
global.init(cx);
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_7);
envGlobals = cx.initStandardObjects(global);
try {
cx.evaluateReader(envGlobals, envReader,
"env.rhino.js", 1, null);
// cx.evaluateReader(envGlobals, jqueryReader,
// "jquery-1.6.2.js", 1, null);
} catch (IOException e) {
}
} finally {
Context.exit();
}
} finally {
try {
envReader.close();
} catch (IOException e) {
}
}
/**
* the above code nicely evaluates env.rhino.js and provides a scope
* object (envGlobals). Then for each script I want to evaluate
* against env.rhino.js's global scope:
*/
Context scriptContext = ContextFactory.getGlobal().enterContext();
try {
// Create a global scope for the dependency we're processing
// and assign our prototype to the environment globals
// (env.js defined globals, the console globals etc.). This
// then allows us to (a) not have to re-establish commonly
// used globals i.e. we can re-use them in our loop; and (b)
// any global assignments are guaranteed to have come from
// the dependency itself (which is what we're trying to
// determine here).
Scriptable globalScope = scriptContext.newObject(envGlobals);
globalScope.setPrototype(envGlobals);
globalScope.setParentScope(null);
scriptContext.setOptimizationLevel(-1);
scriptContext.setLanguageVersion(Context.VERSION_1_7);
try {
//scriptContext.evaluateString(globalScope, "window.location='http://www.amazon.com'", "location", 1, null);
scriptContext.evaluateString(globalScope, tree.toSource(), "script document", 1, null);
System.out.println(scriptContext.toString());
// TODO: Do something useful with the globals.
} finally {
Context.exit();
}
....
Function f = (Function)fObj;
Object result = f.call(scriptContext, globalScope, globalScope, params);
throught this format,i always get the Exception info:
Exception in thread "main" java.lang.NullPointerException
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:849)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
at org.sdc.food.parse.util.JavaScriptParser.getExecutableJS(JavaScriptParser.java:244)
at org.sdc.food.parse.util.JavaScriptParser.main(JavaScriptParser.java:349)
是的,有人帮助我!!
答案 0 :(得分:0)
我已经解决了这个问题。原因是传递给被调用函数的参数是错误的。