代码如下:
public static void main(String[] args)
throws NoSuchMethodException, ScriptException {
String name = "faliang";
ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
String strs = "function say(){return 'hello,'"+name+";}";
jse.eval(strs);
Invocable inv = (Invocable)jse;
String res = (String) inv.invokeFunction("say",name);
System.out.println(res);
}
报错:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ; before statement (<Unknown source>#1) in <Unknown source> at line number 1
顺便问一下,'hello,'"+name+",这一块大概说一下
原本的就是代码是这样:function say(){return 'hello,'+name;}
然后这段又要放在Java中做字符串,name又是变量,所以“function say(){return 'hello,'” 第一段,中间name变量,
“;}”第二段字符串。
嗯嗯 谢谢 已经解决啦