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("JavaScript");

    String script = "var year = 2015";

    engine.eval(script);//from   ww  w. j a va2s . c o  m
    Object year = engine.get("year");
    System.out.println("year's class:" + year.getClass().getName());
    System.out.println("year's value:" + year);
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        System.out.println(String.format("engineName: %s, THREADING: %s", factory.getEngineName(),
                factory.getParameter("THREADING")));
    }// w  ww  . j av a2  s.c  om
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    try {//from  w  w  w .j av  a 2  s.c o m
        FileReader reader = new FileReader("yourFile.js");
        engine.eval(reader);
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        System.out.println(factory.getEngineName());
        System.out.println(factory.getEngineVersion());
        System.out.println(factory.getLanguageName());
        System.out.println(factory.getLanguageVersion());
        System.out.println(factory.getExtensions());
        System.out.println(factory.getMimeTypes());
        System.out.println(factory.getNames());
    }//from w w w. j  av a  2 s  .  c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    String script = "print ('www.java2s.com')";
    try {// w  ww .  jav  a 2s .  co  m
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

From source file:ThreadingBehavior.java

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

    List<ScriptEngineFactory> factories = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factories)
        System.out.println("Threading behavior: " + factory.getParameter("THREADING"));
}

From source file:MetadataDemo.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        System.out.println("Full name = " + factory.getEngineName());
        System.out.println("\nVersion = " + factory.getEngineVersion());
        System.out.println("\nSupported language version = " + factory.getLanguageVersion());
    }//from w  w w  . j  ava  2  s .  c o m
}

From source file:MimeTypesDemo.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = manager.getEngineFactories();
    for (ScriptEngineFactory factory : factories) {
        List<String> mimeTypes = factory.getMimeTypes();
        for (int i = 0; i < mimeTypes.size(); i++) {
            System.out.printf("Supported MIME type " + i + " " + (String) mimeTypes.get(i) + "\n");
        }/*w ww.  j  av a 2s  . c om*/
    }
}

From source file:Main.java

public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {/*ww w.j ava 2  s  .  c o m*/
        engine.put("name", "abcde");
        engine.eval("var output = '';for (i = 0; i <= name.length; i++) {"
                + "  output = name.charAt(i)+'-' + output" + "}");
        String name = (String) engine.get("output");
        System.out.println(name);
    } catch (ScriptException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    engine.put("a", 1);
    engine.put("b", 2);
    try {/*from   w w w.j av a 2 s  .  c  o m*/
        String expression = "(a + b) > 2";
        Object result = engine.eval(expression);
        System.out.println(expression + " ? " + result);
    } catch (ScriptException se) {
        se.printStackTrace();
    }
}