public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("No file specified"); return; } InputStreamReader r = new InputStreamReader(new FileInputStream(args[0])); ScriptEngine engine = new EmbeddedRhinoScriptEngine(); SimpleScriptContext context = new SimpleScriptContext(); engine.put(ScriptEngine.FILENAME, args[0]); engine.eval(r, context); context.getWriter().flush(); }
/** * Executes the program stored in the <code>CompiledScript</code> object using the supplied <code> * Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the associated <code> * ScriptEngine</code> during script execution. If bindings is null, then the effect of calling * this method is same as that of eval(getEngine().getContext()). * * <p>. The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer * </code> associated with the default <code>ScriptContext</code> of the associated <code> * ScriptEngine</code> are used. * * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>. * @return The return value from the script execution * @throws ScriptException if an error occurs. */ public Object eval(Bindings bindings) throws ScriptException { ScriptContext ctxt = getEngine().getContext(); if (bindings != null) { SimpleScriptContext tempctxt = new SimpleScriptContext(); tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE); tempctxt.setBindings( ctxt.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); tempctxt.setWriter(ctxt.getWriter()); tempctxt.setReader(ctxt.getReader()); tempctxt.setErrorWriter(ctxt.getErrorWriter()); ctxt = tempctxt; } return eval(ctxt); }