本文整理了Java中javax.script.ScriptEngine.eval()
方法的一些代码示例,展示了ScriptEngine.eval()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ScriptEngine.eval()
方法的具体详情如下:
包路径:javax.script.ScriptEngine
类名称:ScriptEngine
方法名:eval
[英]Evaluates a script obtained using the specified reader as the script source. Returns null for scripts that don't return a value.
[中]计算使用指定读取器作为脚本源获得的脚本。对于不返回值的脚本,返回null。
代码示例来源: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: stackoverflow.com
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("4*5");
代码示例来源:origin: stackoverflow.com
if(Platform.isMac()) {
final String script="tell application \"System Events\"\n" +
"\tname of application processes whose frontmost is tru\n" +
"end";
ScriptEngine appleScript=new ScriptEngineManager().getEngineByName("AppleScript");
String result=(String)appleScript.eval(script);
System.out.println(result);
}
代码示例来源:origin: shekhargulati/java8-the-missing-tutorial
public static void main(String[] args) throws ScriptException {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine nashorn = factory.getScriptEngine(new NoJavaFilter());
Integer eval = (Integer) nashorn.eval("var HashMap = Java.type('java.util.HashMap')");
System.out.println(eval);
}
代码示例来源:origin: lenskit/lenskit
public static void main(String[] args) throws IOException, ScriptException {
File scriptFile = new File(args[0]);
ScriptEngineManager sem = new ScriptEngineManager();
String ext = Files.getFileExtension(scriptFile.getName());
ScriptEngine engine = sem.getEngineByExtension(ext);
engine.put("cmdArgs", Arrays.asList(args).subList(1, args.length));
try (Reader reader = Files.newReader(scriptFile, Charsets.UTF_8)) {
engine.eval(reader);
}
}
}
代码示例来源:origin: jwpttcg66/NettyGameServer
/**
* 执行指定的脚本内容
*
* @param content 脚本内容
* @param params 执行参数
* @return 脚本执行结果的返回值
*/
public static Object executeScriptContent(String content,
Map<String, Object> params) {
ScriptEngine engine = manager.getEngineByName("JavaScript");
if (params != null) {
for (Map.Entry<String, Object> pair : params.entrySet()) {
engine.put(pair.getKey(), pair.getValue());
}
}
try {
return engine.eval(content);
} catch (ScriptException e) {
logger.error("", e);
}
return null;
}
代码示例来源: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: stackoverflow.com
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("value = 10");
Boolean greaterThan5 = (Boolean) engine.eval("value > 5");
Boolean lessThan5 = (Boolean) engine.eval("value < 5");
System.out.println("10 > 5? " + greaterThan5); // true
System.out.println("10 < 5? " + lessThan5); // false
}
}
代码示例来源:origin: stackoverflow.com
private static final String FUNCTIONS =
"function() {" +
" return \"Hello\";" +
"}";
public static void main(String... args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("text/javascript");
// Compile common functions once
CompiledScript compiled = ((Compilable) engine).compile(FUNCTIONS);
Object sayHello = compiled.eval();
// Load users' script each time
SimpleBindings global = new SimpleBindings();
global.put("sayHello", sayHello);
String script = "sayHello()";
System.out.println(engine.eval(script, global));
}
代码示例来源:origin: qiurunze123/miaosha
private static int calc(String exp) {
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Integer catch1 = (Integer)engine.eval(exp);
return catch1.intValue();
}catch(Exception e) {
e.printStackTrace();
return 0;
}
}
代码示例来源:origin: lenskit/lenskit
private void writeSvgFile(DAGNode<Component, Dependency> graph, File outFile) throws IOException, LenskitCommandException {
StringWriter sw = new StringWriter();
logger.info("writing graph to memory");
GraphDumper.renderGraph(graph, sw);
String dotSrc = sw.toString();
logger.debug("setting up script engine");
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByMimeType("text/javascript");
try (InputStream istr = Graph.class.getResourceAsStream("/META-INF/resources/webjars/viz.js/1.5.1/viz.js");
Reader rdr = new InputStreamReader(istr)) {
logger.debug("loading Viz.js");
engine.put(ScriptEngine.FILENAME, "viz.js");
engine.eval(rdr);
} catch (ScriptException e) {
logger.error("error loading Viz.js", e);
throw new LenskitCommandException("Could not load Viz.js", e);
}
engine.put("dotSrc", dotSrc);
engine.put("outFile", outFile);
try (InputStream istr = Graph.class.getResourceAsStream("render-graph.js");
Reader rdr = new InputStreamReader(istr)) {
logger.info("rendering graph to {}", outFile);
engine.put(ScriptEngine.FILENAME, "render-graph.js");
engine.eval(rdr);
} catch (ScriptException e) {
logger.error("error evaluating render script", e);
throw new LenskitCommandException("could not evaluate SVG renderer", e);
}
}
代码示例来源:origin: jwpttcg66/NettyGameServer
ScriptEngine engine = manager.getEngineByName("JavaScript");
if (params != null) {
for (Map.Entry<String, Object> pair : params.entrySet()) {
engine.put(pair.getKey(), pair.getValue());
reader = new InputStreamReader(new FileInputStream(path),
STRING_CHARSET);
return engine.eval(reader);
} catch (FileNotFoundException | UnsupportedEncodingException | ScriptException e) {
logger.error("", e);
代码示例来源:origin: internetarchive/heritrix3
@Override
public DecideResult innerDecide(CrawlURI uri) {
// 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
try {
engine.put("object",uri);
engine.put("appCtx", appCtx);
return (DecideResult)engine.eval("decisionFor(object)");
} catch (ScriptException e) {
logger.log(Level.WARNING,e.getMessage(),e);
return DecideResult.NONE;
} finally {
engine.put("object", null);
engine.put("appCtx", null);
}
}
}
代码示例来源:origin: shekhargulati/java8-the-missing-tutorial
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine nashorn = manager.getEngineByName("nashorn");
Integer eval = (Integer) nashorn.eval("10 + 20");
System.out.println(eval);
}
}
代码示例来源:origin: stackoverflow.com
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("print('Hello, World')");
代码示例来源:origin: oblac/jodd
protected String run(String html, String query) throws ScriptException {
Jerry doc = Jerry.jerry(html);
scriptEngine.put("doc", doc);
scriptEngine.eval(query);
return doc.html();
}
代码示例来源:origin: stackoverflow.com
import javax.script.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("x", 2);
vars.put("y", 1);
vars.put("z", 3);
System.out.println("result = "+engine.eval("x + y + z", new SimpleBindings(vars)));
}
}
代码示例来源:origin: rapidoid/rapidoid
@SuppressWarnings("unchecked")
public static <T> T eval(String js, Map<String, ?> bindings) throws ScriptException {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
if (bindings != null) {
Map<String, Object> map = U.cast(bindings);
return (T) engine.eval(js, new SimpleBindings(map));
} else {
return (T) engine.eval(js);
}
}
代码示例来源:origin: bluejoe2008/openwebflow
@Override
public void notify(UserDetailsEntity[] users, Task task) throws Exception
{
for (UserDetailsEntity user : users)
{
if (user == null)
continue;
ScriptEngine scriptEngine = new JuelScriptEngineFactory().getScriptEngine();
scriptEngine.put("user", user);
scriptEngine.put("task", task);
String email = user.getProperty(UserDetailsEntity.STRING_PROPERTY_EMAIL);
if (email != null)
{
_mailSender.sendMail(email, (String) scriptEngine.eval(_subjectTemplate),
(String) scriptEngine.eval(_messageTemplate));
}
}
}
代码示例来源:origin: shekhargulati/java8-the-missing-tutorial
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine nashorn = manager.getEngineByName("nashorn");
Bindings bindings = new SimpleBindings(Stream.of(
new SimpleEntry<>("a", 10),
new SimpleEntry<>("b", 20))
.collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue)));
Double eval = (Double) nashorn.eval("a + b", bindings);
System.out.println(eval);
}
}
内容来源于网络,如有侵权,请联系作者删除!