How do I get a ScriptEngine by language name and version?
Category: javax.script, viewed: 1236 time(s).
This example show you how you can obtain a script engine for a specific language name and specific language version. In the code below we try to obtain script engine instance for ECMAScript version 1.6.
package org.kodejava.example.script;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;
public class ScriptEngineSearch {
public static void main(String[] args) {
String languageName = "ECMAScript";
String languageVersion = "1.6";
//
// Creating a ScriptEngineManager and get the list of available
// engine factories.
//
ScriptEngineManager manager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = manager.getEngineFactories();
//
// We obtain a ScriptEngine from the available factories where
// the language name is ECMAScript and the version is 1.6.
// ECMAScript is the standard name for JavaScript programming
// language. If we found the desired language we then get the
// ScriptEngine by calling factory's getScriptEngine() method.
//
ScriptEngine engine = null;
for (ScriptEngineFactory factory : factories) {
String language = factory.getLanguageName();
String version = factory.getLanguageVersion();
if (language.equals(languageName)
&& version.equals(languageVersion)) {
engine = factory.getScriptEngine();
break;
}
}
if (engine != null) {
try {
engine.eval("print('Hello There')");
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
}
Can't find what you are looking for? Join our
FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!