Skip to content

Invoke Script Functions and Methods via JavaScript Engine

List supported script engines

Java
ScriptEngineManager manager = new ScriptEngineManager();
for(ScriptEngineFactory factory : manager.getEngineFactories()){
    System.out.printf("Name: %s%n" +
                    "\tVersion: %s%n" +
                    "\tLanguage name: %s%n" +
                    "\tLanguage version: %s%n" +
                    "\tExtensions: %s%n" +
                    "\tMime types: %s%n" +
                    "\tNames: %s%n",
            factory.getEngineName(),
            factory.getEngineVersion(),
            factory.getLanguageName(),
            factory.getLanguageVersion(),
            factory.getExtensions(),
            factory.getMimeTypes(),
            factory.getNames());
}

Invoke script functions

Java
ScriptEngine jsEngine = manager.getEngineByExtension("js");
// put binding context
jsEngine.put("url", "username=a&category=a book");
try{
    Object result = jsEngine.eval("encodeURI(url)");
    System.out.println(result);
}catch (ScriptException e){
    e.printStackTrace();
}