2

I have tried to call javascript function from java code, but I am getting the following error while using javascript API:

Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "File" is not defined. (#8) at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3770) at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3748) at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3833) at sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1760) at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1785) at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849)

java code:

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    engine.eval(Files.newBufferedReader(Paths.get("D:/test/test.js"), StandardCharsets.UTF_8));
    Invocable inv = (Invocable) engine;

    inv.invokeFunction("display", "test");
    inv.invokeFunction("writeTextFile", "D:\\test\\file.txt", "test");

test.js:

var display = function(name) {
    print("Hello, I am a Javascript display function "+name);
    return "display function return"
}

function writeTextFile(afilename, output) {
    var txtFile = new File(afilename);
    txtFile.writeln(output);
    txtFile.close();
}

display function working fine, the error appear while executing writeTextFile function.

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
user8058941
  • 197
  • 1
  • 1
  • 13
  • Possible duplicate of [Call external javascript functions from java code](https://stackoverflow.com/questions/22856279/call-external-javascript-functions-from-java-code) – dganenco Aug 05 '19 at 14:10
  • maybe see here: https://stackoverflow.com/a/26181292/2924784 – orirab Aug 05 '19 at 14:12
  • Seems like it is due to wrong arguments passed for new File() in the test.js. I think the following would help you get it resolved:https://stackoverflow.com/a/26181292/3981539 – Kavitha Karunakaran Aug 05 '19 at 14:12
  • Hi, I tried the same but same error appears. I seems my missing is the JavaScript libraries as File can't be defined well. But I don't know how to import javaScript lib to java. – user8058941 Aug 05 '19 at 14:45

1 Answers1

0

in your test.js, try:

var txtFile = new File([""], afilename);
AlexC
  • 1,395
  • 14
  • 26
  • Hi, same error. I seems my missing is the JavaScript libraries as File can't be defined well. – user8058941 Aug 05 '19 at 14:18
  • I don't think you will be able to write a file to the local filesystem using javascript called via script engine. See https://w3c.github.io/FileAPI/ for more info (and comments about restrictions and compatibility). And also discussion at https://stackoverflow.com/questions/461791/is-it-possible-to-write-to-a-file-on-a-disk-using-javascript This is essentially a huge security hole, if you execute a script in your java app and someone replaces it with malicious that writes to user disk without approval. Most browsers don't allow you to write to user's disk. – AlexC Aug 06 '19 at 15:30
  • As a work around, call "display", get result and then use Java to create the file for you instead of JavaScript. – AlexC Aug 06 '19 at 15:35