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 manager = new ScriptEngineManager();

    ScriptEngine engine = manager.getEngineByName("js");

    engine.eval("function add (a, b) {c = a + b; return c; }");
    Invocable jsInvoke = (Invocable) engine;

    Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 });
    System.out.println(result1);//from  www . ja va 2 s.c om

    Adder adder = jsInvoke.getInterface(Adder.class);
    int result2 = adder.add(10, 5);
    System.out.println(result2);
}

From source file:ScriptExecutionDemo.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEngine = manager.getEngineByExtension("js");
    jsEngine.eval("println ('Hello! JavaScript executed from a Java program.')");
}

From source file:Main.java

public static void main(String[] args) {

    try {//from w  w  w.  j a v a  2  s  . c  o m

        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine se = sem.getEngineByName("JavaScript");
        String myExpression = "('abc' == 'xyz' && 'thy' == 'thy') || ('ujy' == 'ujy')";
        System.out.println(se.eval(myExpression));

    } catch (ScriptException e) {

        System.out.println("Invalid Expression");
        e.printStackTrace();

    }
}

From source file:RunJavaScript.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {//from   w w  w.  j a  v  a  2s .co m
        Double hour = (Double) engine.eval("var date = new Date();" + "date.getHours();");
        String msg;
        if (hour < 10) {
            msg = "Good morning";
        } else if (hour < 16) {
            msg = "Good afternoon";
        } else if (hour < 20) {
            msg = "Good evening";
        } else {
            msg = "Good night";
        }
        System.out.println(hour);
        System.out.println(msg);
    } catch (ScriptException e) {
        System.err.println(e);
    }
}

From source file:Main.java

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

    Object result = null;//from www  . ja  va2s.  c o m
    result = engine.eval("1 + 2;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5; v = 6;");
    System.out.println(result);
    result = engine.eval("1 + 2; 3 + 4; var v = 5;");
    System.out.println(result);
    result = engine.eval("print(1 + 2)");
    System.out.println(result);

}

From source file:MonthlyPayment.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByExtension("js");
    String calcMonthlyPaymentScript = "intrate = intrate/1200.0;"
            + "payment = principal*intrate*(Math.pow (1+intrate, months)/"
            + "                            (Math.pow (1+intrate,months)-1));";

    engine.put("principal", 20000.0);
    System.out.println("Principal = " + engine.get("principal"));
    engine.put("intrate", 6.0);
    System.out.println("Interest Rate = " + engine.get("intrate") + "%");
    engine.put("months", 360);
    System.out.println("Months = " + engine.get("months"));
    engine.eval(calcMonthlyPaymentScript);
    System.out.printf("Monthly Payment = %.2f\n", engine.get("payment"));
}

From source file:Main.java

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

    String script = "print(msg)";
    try {/* w w w  .  j  av  a2 s  .  c  o m*/
        engine.put("msg", "Hello from Java program");
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

From source file:EnumerateScriptEngines.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();

    List<ScriptEngineFactory> factories = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        System.out.println("Engine name (full): " + factory.getEngineName());
        System.out.println("Engine version: " + factory.getEngineVersion());
        System.out.println("Supported extensions:");
        List<String> extensions = factory.getExtensions();
        for (String extension : extensions)
            System.out.println("  " + extension);
        System.out.println("Language name: " + factory.getLanguageName());
        System.out.println("Language version: " + factory.getLanguageVersion());
        System.out.println("Supported MIME types:");
        List<String> mimetypes = factory.getMimeTypes();
        for (String mimetype : mimetypes)
            System.out.println("  " + mimetype);
        System.out.println("Supported short names:");
        List<String> shortnames = factory.getNames();
        for (String shortname : shortnames)
            System.out.println("  " + shortname);
        System.out.println();//from  www. j  a  va2s. 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("Invoking methods is not supported.");
        return;//from w w  w. j ava 2 s  .  c  o m
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "c:/Java_Dev/calculator.js";

    engine.eval("load('" + scriptPath + "')");

    Object calculator = engine.get("calculator");

    int x = 3;
    int y = 4;
    Object addResult = inv.invokeMethod(calculator, "add", x, y);
    Object subResult = inv.invokeMethod(calculator, "subtract", x, y);
    Object mulResult = inv.invokeMethod(calculator, "multiply", x, y);
    Object divResult = inv.invokeMethod(calculator, "divide", x, y);

    System.out.println(addResult);
    System.out.println(subResult);
    System.out.println(mulResult);
    System.out.println(divResult);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xyz = "3*3+3";
    String kkk = "(100 % 6)* 7";
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine se = manager.getEngineByName("JavaScript");
    Object result1 = se.eval(xyz);
    Object result2 = se.eval(kkk);
    System.out.println("result1: " + result1);
    System.out.println("result2: " + result2);
}