On Dec 23, 11:04 am, "Shauvik Roy Choudhary" <[email protected]> wrote: > Line 76 of Shell.java, explains that the functions "print", "quit", > "version", "load", "help" are not part of ECMA. > So, you'll have to define them in your source file. > > *What i do:* > > Step 1: Define the print function in MyClass (as in Shell.java) > > Step 2: > Approach 1: Using the rhino engine in Java > ScriptEngineManager manager = new ScriptEngineManager(); > ScriptEngine engine = manager.getEngineByName("js"); > try { > engine.eval(new FileReader(new File("test.js"))); > } catch (ScriptException e1) { > e1.printStackTrace(); > } catch (FileNotFoundException e) { > e.printStackTrace(); > } > > Approach 2: Using rhino form your own jar/source > Context cx = Context.enter(); > ScriptableObject scope = cx.initStandardObjects(); > > String[] names = { "print"}; > scope.defineFunctionProperties(names, RunJS1.class, > ScriptableObject.DONTENUM); > try { > cx.evaluateReader(scope, new FileReader("test.js"), "test.js", > 0, null); > } catch (FileNotFoundException e) { > e.printStackTrace(); > } catch (IOException e) { > e.printStackTrace(); > } > > Any other suggestions/comments? > > Cheers, > Shauvik > > On Tue, Dec 23, 2008 at 8:54 AM, <[email protected]> wrote: > > Hi, > > > I just started with the Rhino and it's samples. > > > I'm trying to run the enum.js that comes with the examples and > > contains the following: > > var array = [0, 1, 2]; > > > // create an array enumeration. > > var elements = new java.util.Enumeration({ > > index: 0, > > elements: array, > > hasMoreElements: function() { > > return (this.index < this.elements.length); > > }, > > nextElement: function() { > > return this.elements[this.index++]; > > } > > }); > > > // now print out the array by enumerating through the Enumeration > > while (elements.hasMoreElements()) > > print(elements.nextElement()); > > > I can run it with the shell but when trying to use the: > > cx.evaluateReader(scope, new FileReader("enum.js"), "enum.js", 1, > > null); > > > I get the following error: > > Exception in thread "main" org.mozilla.javascript.EcmaError: > > ReferenceError: "print" is not defined. > > > Is that the right way to run js files? or should I use another way? > > Where can I see examples how to run compiled javascript files from a > > java program? > > > Thanks In advance! > > > _______________________________________________ > > dev-tech-js-engine-rhino mailing list > > [email protected] > >https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
Or, just run the examples in the Rhino shell, which has print (and other top-level functions) defined. --Norris _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
