Example usage for javax.script ScriptEngineManager getEngineByExtension

List of usage examples for javax.script ScriptEngineManager getEngineByExtension

Introduction

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

Prototype

public ScriptEngine getEngineByExtension(String extension) 

Source Link

Document

Look up and create a ScriptEngine for a given extension.

Usage

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:ObtainScriptEngine.java

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

    ScriptEngine engine1 = manager.getEngineByExtension("js");
    System.out.println(engine1);/*from w w  w  .j a  va 2 s  .co  m*/

    ScriptEngine engine2 = manager.getEngineByMimeType("application/javascript");
    System.out.println(engine2);

    ScriptEngine engine3 = manager.getEngineByName("rhino");
    System.out.println(engine3);
}

From source file:JavaObjectDemo.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEngine;//w ww .  ja v a2s.  c  o m
    jsEngine = manager.getEngineByExtension("js");
    jsEngine.eval("importPackage(javax.swing);var optionPane =JOptionPane.showMessageDialog(null, 'Hello!);");
}

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:ScriptExecutionReaderDemo.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEengine = manager.getEngineByExtension("js");
    Reader reader = new InputStreamReader(new FileInputStream("yourJavaScript.js"));
    jsEengine.eval(reader);/*  w ww . j  a v  a  2s .c  o  m*/
}

From source file:GetEngineByExtensionDemo.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    /* Retrieve a ScriptEngine that supports scripts with .js extension */
    ScriptEngine jsEngine = manager.getEngineByExtension("js");
    System.out.println(jsEngine);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    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));

    Bindings bindings = engine.createBindings();
    bindings.put("engine", "overridden engine bindings");
    bindings.put("bindings", bindings);
    engine.eval("app.dumpBindings (bindings);", bindings);

    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: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 {/* w  ww.  j a v a  2 s  . c  o  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:com.xafero.vee.cmd.MainApp.java

private static void execute(String fileName) throws FileNotFoundException, ScriptException {
    File file = (new File(fileName)).getAbsoluteFile();
    if (!file.exists())
        throw new FileNotFoundException("There's no file named '" + file + "'!");
    String extension = Files.getExtension(file);
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByExtension(extension);
    Bindings env = engine.createBindings();
    inject(env, file);//from   w  w w.  ja va  2  s  . c  o m
    try {
        Object result = engine.eval(new FileReader(file), env);
        if (result != null)
            System.out.println(result.toString());
    } catch (ScriptException e) {
        throw e;
    }
}

From source file:de.hasait.genesis.scriptgen.ScriptGenProcessor.java

private ScriptEngine determineScriptEngine(final String pScriptFileExtension, final ClassLoader pClassLoader) {
    final ScriptEngine engine;
    final NashornScriptEngineFactory nashornScriptEngineFactory = new NashornScriptEngineFactory();
    if (nashornScriptEngineFactory.getExtensions().contains(pScriptFileExtension)) {
        engine = nashornScriptEngineFactory.getScriptEngine(pClassLoader);
    } else {//from   w  w  w.  j  a v  a  2 s. co m
        final ScriptEngineManager factory = new ScriptEngineManager();
        engine = factory.getEngineByExtension(pScriptFileExtension);
    }
    return engine;
}