Example usage for javax.script ScriptEngineManager ScriptEngineManager

List of usage examples for javax.script ScriptEngineManager ScriptEngineManager

Introduction

In this page you can find the example usage for javax.script ScriptEngineManager ScriptEngineManager.

Prototype

public ScriptEngineManager() 

Source Link

Document

The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()).

Usage

From source file:Main.java

public static void main(String[] args) {
    // Get the script engine factory for Nashorn
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    ScriptEngineFactory factory = engine.getFactory();

    // Get the script
    String script = factory.getOutputStatement("\"java2s\"");
    System.out.println("Syntax: " + script);

    // Evaluate the script
    try {//from  w  ww  . j  a va2 s  .c  o m
        engine.eval(script);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext defaultCtx = engine.getContext();
    // Work with defaultCtx here

    // Create a new context
    ScriptContext ctx = new SimpleScriptContext();

    // Configure ctx here

    // Set ctx as the new default context for the engine
    engine.setContext(ctx);/*from ww  w  . j a v  a 2  s  .co m*/

    ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE);

    engine.setContext(ctx);
}

From source file:GetToKnowBindingsAndScopes.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");

    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");

    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));

    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));

    try {/*from  w w  w.  j ava2 s . co  m*/
        Bindings bindings = engine.createBindings();
        bindings.put("engine", "overridden engine bindings");
        bindings.put("app", new GetToKnowBindingsAndScopes());
        bindings.put("bindings", bindings);
        engine.eval("app.dumpBindings (bindings);", bindings);
    } catch (ScriptException se) {
        System.err.println(se.getMessage());
    }

    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");

    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
}

From source file:TestCompilationSpeed.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    String fact = "function fact (n){if (n == 0)return 1; else return n*fact (n-1);};";

    long time = System.currentTimeMillis();
    for (int i = 0; i < MAX_ITERATIONS; i++)
        engine.eval(fact);/*from   w  w  w. j  a  va  2 s. c  om*/
    System.out.println(System.currentTimeMillis() - time);

    Compilable compilable = null;
    if (engine instanceof Compilable) {
        compilable = (Compilable) engine;
        CompiledScript script = compilable.compile(fact);

        time = System.currentTimeMillis();
        for (int i = 0; i < MAX_ITERATIONS; i++)
            script.eval();
        System.out.println(System.currentTimeMillis() - time);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    engine.eval("var msg = 'Hello globals'");
    engine.eval("print(this.msg);");
    ScriptContext ctx = new SimpleScriptContext();
    ScriptContext defaultCtx = engine.getContext();
    Bindings engineBindings = defaultCtx.getBindings(ENGINE_SCOPE);
    ctx.setBindings(engineBindings, ENGINE_SCOPE);
    engine.eval("print(this.msg);", ctx);
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    Main result = new Main();
    engine.put("result", result);
    String script = "3 + 4; result.setValue(1);";
    engine.eval(script);/*from   w  ww  . j a  va  2 s. c  o m*/
    int returnedValue = result.getValue();
    System.out.println("Returned value is " + returnedValue);
}

From source file:Main.java

public static void main(String[] args) {
    String scriptFileName = "c:/test.js";
    Path scriptPath = Paths.get(scriptFileName);
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    try {/* w w w  .j  a v a2  s .  c  o  m*/
        Reader scriptReader = Files.newBufferedReader(scriptPath);
        engine.eval(scriptReader);
    } catch (IOException | ScriptException e) {
        e.printStackTrace();
    }
}

From source file:FuncEvaluator.java

  public static void main(String[] args) {
  if (args.length != 2) {
    System.err/*w w  w .j  a v  a 2 s .co m*/
        .println("usage: java FuncEvaluator scriptfile " + "script-exp");
    return;
  }

  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine engine = manager.getEngineByName("rhino");

  try {
    System.out.println(engine.eval(new FileReader(args[0])));
    System.out.println(engine.eval(args[1]));
  } catch (ScriptException se) {
    System.err.println(se.getMessage());
  } catch (IOException ioe) {
    System.err.println(ioe.getMessage());
  }
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    manager.put("n1", 1);
    String script = "var sum = n1 + n2;print(msg + " + "' n1=' + n1 + ', n2=' + n2 + " + "', sum=' + sum);";

    engine.put("n2", 2);
    engine.put("msg", "a string");
    engine.eval(script);//from  w  ww . j a  v  a2  s . c o m

    Bindings bindings = engine.createBindings();
    bindings.put("n2", 3);
    bindings.put("msg", "another string");
    engine.eval(script, bindings);

    ScriptContext ctx = new SimpleScriptContext();
    Bindings ctxGlobalBindings = engine.createBindings();
    ctx.setBindings(ctxGlobalBindings, GLOBAL_SCOPE);
    ctx.setAttribute("n1", 4, GLOBAL_SCOPE);
    ctx.setAttribute("n2", 5, ENGINE_SCOPE);
    ctx.setAttribute("msg", "ScriptContext:", ENGINE_SCOPE);
    engine.eval(script, ctx);

    engine.eval(script);
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");

    String[] ops = { "+", "-", "*", "/" };

    JPanel gui = new JPanel(new BorderLayout(2, 2));
    JPanel labels = new JPanel(new GridLayout(0, 1));
    gui.add(labels, BorderLayout.WEST);
    labels.add(new JLabel("a"));
    labels.add(new JLabel("operand"));
    labels.add(new JLabel("b"));
    labels.add(new JLabel("="));

    JPanel controls = new JPanel(new GridLayout(0, 1));
    gui.add(controls, BorderLayout.CENTER);
    JTextField a = new JTextField(10);
    controls.add(a);//from   w w  w . j  a  va  2 s  .  c o m
    JComboBox operand = new JComboBox(ops);
    controls.add(operand);
    JTextField b = new JTextField(10);
    controls.add(b);
    JTextField output = new JTextField(10);
    controls.add(output);

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String expression = a.getText() + operand.getSelectedItem() + b.getText();
            try {
                Object result = engine.eval(expression);
                if (result == null) {
                    output.setText("Output was 'null'");
                } else {
                    output.setText(result.toString());
                }
            } catch (ScriptException se) {
                output.setText(se.getMessage());
            }
        }
    };

    operand.addActionListener(al);
    a.addActionListener(al);
    b.addActionListener(al);

    JOptionPane.showMessageDialog(null, gui);
}