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) throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> engines = mgr.getEngineFactories();
    for (ScriptEngineFactory engine : engines) {
        System.out.println(engine.getEngineName());
        for (String n : engine.getNames()) {
            System.out.println("Short name : " + n);
        }//from  w  w  w  .j  a  va  2 s .c  o m
    }
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String myJSCode = "function myFunction(){return (4+2);}myFunction();";
    System.out.println(engine.eval(myJSCode));
}

From source file:ListEngineFactoryDemo.java

public static void main(String[] args) {
    // create ScriptEngineManager
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> factoryList = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factoryList) {
        System.out.println(factory.getEngineName());
        System.out.println(factory.getLanguageName());
    }// www . java  2  s  . c  om
}

From source file:Main.java

public static void main(String[] args) {
    // Create a script engine manager
    ScriptEngineManager manager = new ScriptEngineManager();

    // Obtain a script engine from the manager
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    // Store the script in a String
    String script = "print('hi from java2s.com!')";
    try {//  ww w  .  j ava2 s.  c  o  m
        // Execute the script
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

From source file:InvocableTest.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {/*from  w  w w.j  av a2 s  .co m*/
        engine.eval("function myFunction(name){var output = '';"
                + "  for (i = 0; i <= name.length; i++) {output = name.charAt(i)+'-'+ output"
                + "  } return output;}");
        Invocable invokeEngine = (Invocable) engine;
        Object o = invokeEngine.invokeFunction("myFunction", "abcde");
        System.out.println(o);

    } catch (NoSuchMethodException e) {
        System.err.println(e);
    } catch (ScriptException e) {
        System.err.println(e);
    }
}

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();
    engine.eval("print(this.msg);", ctx);
}

From source file:Main.java

public static void main(String[] args) {
    // Get the script engine manager
    ScriptEngineManager manager = new ScriptEngineManager();

    // Try executing scripts in Nashorn, Groovy, Jython, and JRuby
    execute(manager, "JavaScript", "print('Hello JavaScript')");
    execute(manager, "Groovy", "println('Hello Groovy')");
    execute(manager, "jython", "print 'Hello Jython'");
    execute(manager, "jruby", "puts('Hello JRuby')");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 5);

    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    Object a = bindings.get("a");
    Object b = bindings.get("b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);

    Object result = engine.eval("c = a + b;");
    System.out.println("a + b = " + result);
}

From source file:InterfaceTest.java

public static void main(String args[]) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    engine.eval("function run() {print('www.java2s.com');}");
    Invocable invokeEngine = (Invocable) engine;
    Runnable runner = invokeEngine.getInterface(Runnable.class);
    Thread t = new Thread(runner);
    t.start();//from ww w .  j av a  2s. c  o m
    t.join();
}

From source file:CompilableDemo.java

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

    Compilable jsCompile = (Compilable) engine;
    CompiledScript script = jsCompile.compile("function hi () {print ('www.java2s.com !'); }; hi ();");

    for (int i = 0; i < 5; i++) {
        script.eval();/* w w  w.ja v a2s. c  o  m*/
    }
}

From source file:Main.java

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

    if (!(engine instanceof Invocable)) {
        System.out.println("Interface implementation in script" + "is not supported.");
        return;//  w w  w  .  j a v a2 s.c om
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "c:/Java_Dev/cal.js";
    engine.eval("load('" + scriptPath + "')");
    Calculator calc = inv.getInterface(Calculator.class);
    if (calc == null) {
        System.err.println("Calculator interface " + "implementation not found.");
        return;
    }
    double x = 2.0;
    double y = 1.0;
    double addResult = calc.add(x, y);

    System.out.println(addResult);
}