1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 import org.mozilla.javascript.*;
8 
9 /**
10  * RunScript: simplest example of controlling execution of Rhino.
11  *
12  * Collects its arguments from the command line, executes the
13  * script, and prints the result.
14  *
15  * @author Norris Boyd
16  */
17 public class RunScript {
main(String args[])18     public static void main(String args[])
19     {
20         // Creates and enters a Context. The Context stores information
21         // about the execution environment of a script.
22         Context cx = Context.enter();
23         try {
24             // Initialize the standard objects (Object, Function, etc.)
25             // This must be done before scripts can be executed. Returns
26             // a scope object that we use in later calls.
27             Scriptable scope = cx.initStandardObjects();
28 
29             // Collect the arguments into a single string.
30             String s = "";
31             for (int i=0; i < args.length; i++) {
32                 s += args[i];
33             }
34 
35             // Now evaluate the string we've colected.
36             Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
37 
38             // Convert the result to a string and print it.
39             System.err.println(Context.toString(result));
40 
41         } finally {
42             // Exit from the context.
43             Context.exit();
44         }
45     }
46 }
47 
48