Java by API/javax.script/ScriptEngineManager — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:35, 31 мая 2010

new ScriptEngineManager()

     
import java.util.List;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
public class Main {
  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());
    }
  }
}





ScriptEngineManager: getEngineByMimeType(String mimeType)

   
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main{
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEngine = manager.getEngineByMimeType("text/javascript");
    if (!(jsEngine == null)) {
      System.out.println("text/javascript MIME type retrieved:" + jsEngine);
    }
    ScriptEngine jsEngine2 = manager.getEngineByMimeType("text/vbscript");
    if (jsEngine2 == null)
      System.out.println("\nNo supported script engine found for text/vbscript MIME type.");
  }
}





ScriptEngineManager: getEngineByName(String shortName)

   
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    String script = "print ("www.jexp.ru")";
    try {
      engine.eval(script);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}