ReadFully not defined using Java Nashorn Javascript Engine

I am trying to run a javascript script with the new Java 8 Nashorn javascript engine, but it does not work with the following error:

<eval>:1 ReferenceError: "readFully" is not defined 

The script uses the readFully function, which must be defined in the global scope. nashorn starts with the scripting mode enabled (which is used by default when working with ScriptEngine, as shown here http://mail.openjdk.java.net/pipermail/nashorn-dev/2013-December/002562.html ).

Here is an example to reproduce the error:

 import java.io.FileNotFoundException; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Test { public static void main(String[] argv) throws FileNotFoundException, ScriptException { ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn"); scriptEngine.eval("print('Hey!');print(print);print(readFully);"); } } 

This sample prints hey! and then the source code of the print function (another nashorn built-in function) and finally, it should print the source code of the readFully method. But instead, I have this exception:

 Exception in thread "main" javax.script.ScriptException: ReferenceError: "readFully" is not defined in <eval> at line number 1 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521) at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) at com.github.bringking.maven.requirejs.Test.main(Test.java:14) Caused by: <eval>:1 ReferenceError: "readFully" is not defined at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:58) at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:320) at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:292) at jdk.nashorn.api.scripting.NashornScriptEngine.__noSuchProperty__(NashornScriptEngine.java:272) at jdk.nashorn.internal.scripts.Script$engine.L:35(nashorn:engine/resources/engine.js:37) at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1) at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535) at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209) at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568) ... 5 more 

When the sample script is run with the nashorn command line with the -scripting option (using the jdk jjs tool), everything is fine. Here is the result of the same script:

 Hey! function print() { [native code] } function readFully() { [native code] } 

I could rewrite the readFully method and associate it with the script context, but I prefer to understand why it does not work and uses already built-in functions.

Hello

+6
source share
4 answers

Finally, I implemented the readFully function, which I use in my script (only compatible with Nashorn):

 function readFully(url) { var result = ""; var imports = new JavaImporter(java.net, java.lang, java.io); with (imports) { var urlObj = null; try { urlObj = new URL(url); } catch (e) { // If the URL cannot be built, assume it is a file path. urlObj = new URL(new File(url).toURI().toURL()); } var reader = new BufferedReader(new InputStreamReader(urlObj.openStream())); var line = reader.readLine(); while (line != null) { result += line + "\n"; line = reader.readLine(); } reader.close(); } return result; } 
+4
source

readFully is not a standard JavaScript function, and it is probably not standard in Nashborne.

There were problems similar problems when Rhino was chosen to be included in the Sun Java 6 implementation. A scripting tool may provide improvements that are not available in the built-in API. readFully is not a documented feature in the Java 8 Nashorn API .

In previous versions of Java, the specification indicated that provided that scripting mechanisms were implementation details of the JRE provider. I don’t know if Java 8 will do anything with the required mechanisms to ensure future compatibility guarantees. I would fully test JSR-337 if this could be a problem.

+2
source

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

readFully (script only)

This function reads the entire contents of the file passed as a string argument and sends it to stdout, or you can assign the result to a variable.

readFully example:

jjs> readFully("text.txt") This is the contents of the text.txt file located in the current working directory.

+1
source

readFully is enabled only in script mode. Nashorn Docs

try the following:

 >>jjs -scripting jjs> readFully("your_file") 
0
source

Source: https://habr.com/ru/post/980547/


All Articles