i'm trying to call document in java using scriptEngine here is my code
ScriptEngineManager scriptEngineManager=new ScriptEngineManager();
ScriptEngine scriptEngine=scriptEngineManager.getEngineByName("nashorn");
scriptEngine.eval("function func(){" +
"document.location.href=someUrl;" +
"var text=document.getElementsByTagName('textarea')[0];"}"
);
Invocable invocable= (Invocable) scriptEngine;
((Invocable) scriptEngine).invokeFunction("func");
it throws an exception
javax.script.ScriptException: ReferenceError: "document" is not defined in <eval> at line number 1
The script engine in Java provides the ability to execute Javascript code, but does not provide an emulation of a browser.
document
,window
and other objects are defined by the browsers but are not mandated to be present by ECMAScript specification (http://www.ecma-international.org/ecma-262/5.1/#sec-15).