本文整理了Java中javax.script.ScriptEngine
类的一些代码示例,展示了ScriptEngine
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ScriptEngine
类的具体详情如下:
包路径:javax.script.ScriptEngine
类名称:ScriptEngine
[英]See Javadoc of Java Scripting API
[中]参见Java Scripting API的Javadoc
代码示例来源:origin: stackoverflow.com
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// read script file
engine.eval(Files.newBufferedReader(Paths.get("C:/Scripts/Jsfunctions.js"), StandardCharsets.UTF_8));
Invocable inv = (Invocable) engine;
// call function from script file
inv.invokeFunction("yourFunction", "param");
代码示例来源:origin: internetarchive/heritrix3
@Override
protected void innerProcess(CrawlURI curi) {
// depending on previous configuration, engine may
// be local to this thread or shared
ScriptEngine engine = getEngine();
synchronized(engine) {
// synchronization is harmless for local thread engine,
// necessary for shared engine
engine.put("curi",curi);
engine.put("appCtx", appCtx);
try {
engine.eval("process(curi)");
} catch (ScriptException e) {
logger.log(Level.WARNING,e.getMessage(),e);
} finally {
engine.put("curi", null);
engine.put("appCtx", null);
}
}
}
代码示例来源:origin: Netflix/conductor
public static Object eval(String script, Object input) throws ScriptException {
Bindings bindings = engine.createBindings();
bindings.put("$", input);
return engine.eval(script, bindings);
}
}
代码示例来源:origin: plantuml/plantuml
public String eval(String line) throws ScriptException {
final ScriptContext context = engine.getContext();
final StringWriter sw = new StringWriter();
context.setWriter(new PrintWriter(sw));
engine.eval(line, context);
final String result = sw.toString();
return result;
}
}
代码示例来源:origin: castagna/jena-examples
public static void main(String[] args) throws FileNotFoundException {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine ruby = sem.getEngineByExtension("rb");
try {
FileReader reader = new FileReader(new File("src/main/jruby", "hello_jena.rb"));
ruby.eval(reader);
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
}
代码示例来源:origin: apache/accumulo
ScriptEngineManager mgr = new ScriptEngineManager();
engineName = cl.getOptionValue(engine.getOpt());
ScriptEngine engine = mgr.getEngineByName(engineName);
if (engine == null) {
shellState.printException(new Exception(engineName + " not found"));
Bindings b = engine.getBindings(ScriptContext.ENGINE_SCOPE);
putConnector(b, shellState.getAccumuloClient());
b.put("client", shellState.getAccumuloClient());
ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
Object[] argArray = argValues.toArray(new Object[argValues.size()]);
return 1;
Reader reader = new FileReader(f);
try {
engine.eval(reader, ctx);
if (invoke) {
this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
engine.eval(inlineScript, ctx);
if (invoke) {
this.invokeFunctionOrMethod(shellState, engine, cl, argArray);
代码示例来源:origin: stackoverflow.com
ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType( "text/javascript" );
Bindings bindings = engine.getBindings( ScriptContext.GLOBAL_SCOPE );
bindings.put( "varname", ... );
bindings.put( ... );
engine.put( ScriptEngine.FILENAME, script.toString());
engine.eval( new FileReader( script ));
代码示例来源:origin: lenskit/lenskit
ScriptEngineManager sem = new ScriptEngineManager();
File scriptFile = options.get("script");
String ext = Files.getFileExtension(scriptFile.getName());
ScriptEngine seng = sem.getEngineByExtension(ext);
logger.info("running {} with engine {}", scriptFile, seng);
SimpleBindings bindings = new SimpleBindings();
bindings.put("logger", LoggerFactory.getLogger(scriptFile.getName()));
bindings.put("args", options.<List<String>>get("args"));
bindings.put("recommender", rec);
try (Reader reader = new FileReader(scriptFile)) {
seng.eval(reader, bindings);
} catch (IOException e) {
throw new LenskitCommandException("error loading script file", e);
代码示例来源:origin: stackoverflow.com
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Test {
public static void main(String[] args) throws Exception{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}
代码示例来源:origin: jphp-group/jphp
@Test
public void testVariableGet() throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("jphp");
engine.eval("<?php $baz = 'test value';");
Assert.assertEquals("test value", engine.get("baz").toString());
}
代码示例来源:origin: wildfly/wildfly
protected Map<String,String> parseJsonResponse(String json) throws ScriptException
{
synchronized (scriptEngineLock)
{
if (scriptEngine == null)
{
scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
if (scriptEngine == null) {
throw new RuntimeException("Failed to load JavaScript script engine");
}
}
Bindings bindings = new SimpleBindings();
bindings.put("json", json);
return (Map<String, String>)scriptEngine.eval(JSON_RESPONSE_PARSING_SCRIPT, bindings);
}
}
}
代码示例来源:origin: RaiMan/SikuliX2
private boolean runJS() {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
String scriptBefore =
"var Do = Java.type('com.sikulix.api.Do');\n" +
"var SX = Java.type('com.sikulix.core.SX');\n" +
"log = SX.getSXLog(\"SX.JAVASCRIPTRUNNER\");\n";
if (withTrace) {
scriptBefore += "log.on(SX.TRACE);\n";
}
String scriptText = scriptBefore;
scriptText += script;
log.trace("%s: running script %s", ScriptType.JAVASCRIPT, scriptName);
if (withTrace) {
log.p(script);
log.p("---------- end of script");
}
Object returned = null;
try {
returned = engine.eval(scriptText);
} catch (ScriptException e) {
log.trace("%s: error: %s", ScriptType.JAVASCRIPT, e.getMessage());
returnObject = new ReturnObject(false);
return false;
}
log.trace("%s: ending run", ScriptType.JAVASCRIPT);
returnObject = new ReturnObject(ScriptType.JAVASCRIPT, returned);
return true;
}
代码示例来源:origin: apache/tinkerpop
public boolean test(final Object a) {
try {
final Bindings bindings = new SimpleBindings();
bindings.put(A, a);
return (boolean) this.engine.eval(this.script, bindings);
} catch (final ScriptException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
代码示例来源:origin: stackoverflow.com
import javax.script.*;
public class ScriptRunner {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
String scriptName = args[0];
Bindings bindings = nashorn.createBindings();
bindings.put("scriptFileName", scriptName);
nashorn.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
nashorn.eval("load('src/main/resources/javascript/' + scriptFileName)");
}
代码示例来源:origin: jphp-group/jphp
@Test
public void testVariableSet() throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("jphp");
engine.put("foo", "bar");
Object value = engine.eval("<?php return $foo;");
Assert.assertEquals("bar", value.toString());
}
代码示例来源:origin: stackoverflow.com
String javaScriptExpression = "sayHello(name);";
Reader javaScriptFile = new StringReader(
"function sayHello(name) {\n"
+ " println('Hello, '+name+'!');\n" + "}");
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory
.getEngineByName("JavaScript");
ScriptContext context = engine.getContext();
context.setAttribute("name", "JavaScript",
ScriptContext.ENGINE_SCOPE);
engine.eval(javaScriptFile);
engine.eval(javaScriptExpression);
代码示例来源:origin: stackoverflow.com
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
ScriptContext ctx = new SimpleScriptContext();
ctx.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
engine.eval("function hello() { return 'Hello!'; }", ctx);
engine.setContext(ctx);
((Invocable) engine).invokeFunction("hello");
代码示例来源:origin: stackoverflow.com
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
ScriptContext context = engine.getContext();
StringWriter writer = new StringWriter();
context.setWriter(writer);
engine.eval("print('Welocme to java worldddd')");
String output = writer.toString();
System.out.println("Script output: " + output);
代码示例来源:origin: apache/nifi
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
if (bindings == null) {
bindings = new SimpleBindings();
bindings.put("context", context);
bindings.put("log", log);
bindings.put("vmMetrics", vmMetrics);
scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
configurator.eval(scriptEngine, scriptToRun, scriptingComponentHelper.getModules());
} else {
scriptEngine.eval(scriptToRun);
代码示例来源:origin: stackoverflow.com
import java.util.*;
import javax.script.*;
public class TestBindings {
public static void main(String args[]) throws Exception {
String script = "function doSomething() {var d = date}";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
Compilable compilingEngine = (Compilable) engine;
CompiledScript cscript = compilingEngine.compile(script);
//Bindings bindings = cscript.getEngine().createBindings();
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
for(Map.Entry me : bindings.entrySet()) {
System.out.printf("%s: %s\n",me.getKey(),String.valueOf(me.getValue()));
}
bindings.put("date", new Date());
//cscript.eval();
cscript.eval(bindings);
Invocable invocable = (Invocable) cscript.getEngine();
invocable.invokeFunction("doSomething");
}
}
内容来源于网络,如有侵权,请联系作者删除!