我得到此错误,我将运行以下java文件。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestForTheThread {
public static void main(String args[]) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
try {
URL yahoo = new URL("http://localhost/XMLLoadProject/thiru.js");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
engine.eval(in);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("fail to load");
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("fail to load");
}
Invocable invocable = (Invocable) engine;
Object result;
try {
result = invocable.invokeFunction("idplogurl", "thriu");
System.out.println(result);
} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
示例js文件
var idplogurl = function(idpcode) {
/*! jQuery v1.11.2 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license */
document.write('<script type="text/javascript" src="'+ idpcode+ '"></script>');
}
在javascript文件中(&#39; thiru.js&#39;)包含文档(javascript默认对象)。 一些网站说用浏览器不可能。 任何人请对此错误提出质疑。 我收到了这个错误
javax.script.ScriptException:sun.org.mozilla.javascript.internal.EcmaError:ReferenceError:&#34; document&#34;没有定义。 (#17)在第17行
请帮助我任何人......
答案 0 :(得分:0)
尝试使用此案例或驼峰案例
ScriptEngine engine = mgr.getEngineByName("JavaScript");
or
ScriptEngine engine = mgr.getEngineByName("javaScript");
粘贴您的javascript代码
尝试this您可能会有所了解。