readFully没有使用Java Nashorn Javascript Engine定义

时间:2022-12-14 21:09:40

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

我正在尝试使用新的Java 8 Nashorn javascript引擎运行javascript脚本,但它失败并出现以下错误:

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

The script uses the readFully function that should be defined in the global scope nashorn is run with the scripting mode enabled (wich is default when running through a ScriptEngine as seen here http://mail.openjdk.java.net/pipermail/nashorn-dev/2013-December/002562.html).

该脚本使用应该在全局范围内定义的readFully函数nashorn在启用脚本模式的情况下运行(这是通过ScriptEngine运行时的默认值,如http://mail.openjdk.java.net/pipermail/nashorn-开发/ 2013-月/ 002562.html)。

Here is a sample 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 I have this Exception instead:

这个样本打印嘿!然后是print函数的源代码(另一个nashorn内置函数),最后它应该打印readFully方法的源代码。但我有这个例外:

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 parameter (with the jjs tool of the jdk), all is fine. Here is the result of the same script:

当使用带有-scripting参数的nashorn命令行(使用jdk的jjs工具)运行示例脚本时,一切都很好。这是相同脚本的结果:

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

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

我可以重写一个readFully方法并将其与脚本上下文绑定,但我更喜欢理解为什么它不起作用并使用已经内置的函数。

Regards

4 个解决方案

#1


5  

Finally, I have implemented a readFully function that I use in my script (Only compatible with Nashorn):

最后,我实现了一个我在脚本中使用的readFully函数(仅与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;
}

#2


2  

readFully is not a standard JavaScript function and it is likely not standard in Nashorn either.

readFully不是标准的JavaScript函数,它也可能不是Nashorn的标准。

There were similar issues when Rhino was chosen for inclusion in the Sun implementation of Java 6. The scripting tool may provide enhancements that are not present in the embedded API. readFully is not a documented function in the Java 8 Nashorn API.

当Rhino被选择包含在Java 6的Sun实现中时,存在类似的问题。脚本工具可能提供嵌入式API中不存在的增强功能。 readFully不是Java 8 Nashorn API中的文档化函数。

In previous versions of Java the specification stated that provided scripting engines were an implementation detail of the JRE vendor. I am not aware if Java 8 makes anything about the engines provided mandatory or whether it makes any future compatibility guarantees. I would check JSR-337 thoroughly if this was likely to be an issue.

在以前的Java版本中,规范声明提供的脚本引擎是JRE供应商的实现细节。我不知道Java 8是否对所提供的引擎有任何强制性要求,或者它是否会提供任何未来的兼容性保证。如果这可能是一个问题,我会彻底检查JSR-337。

#3


1  

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

readFully (-scripting mode only)

readFully(仅限脚本模式)

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

此函数读取作为字符串参数传入的文件的全部内容并将其发送到stdout,或者您可以将结果分配给变量。

readFully example:

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

jjs> readFully(“text.txt”)这是位于当前工作目录中的text.txt文件的内容。

#4


0  

readFully is enabled only in scripting mode. Nashorn docs

readFully仅在脚本模式下启用。 Nashorn文档

try this:

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

#1


5  

Finally, I have implemented a readFully function that I use in my script (Only compatible with Nashorn):

最后,我实现了一个我在脚本中使用的readFully函数(仅与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;
}

#2


2  

readFully is not a standard JavaScript function and it is likely not standard in Nashorn either.

readFully不是标准的JavaScript函数,它也可能不是Nashorn的标准。

There were similar issues when Rhino was chosen for inclusion in the Sun implementation of Java 6. The scripting tool may provide enhancements that are not present in the embedded API. readFully is not a documented function in the Java 8 Nashorn API.

当Rhino被选择包含在Java 6的Sun实现中时,存在类似的问题。脚本工具可能提供嵌入式API中不存在的增强功能。 readFully不是Java 8 Nashorn API中的文档化函数。

In previous versions of Java the specification stated that provided scripting engines were an implementation detail of the JRE vendor. I am not aware if Java 8 makes anything about the engines provided mandatory or whether it makes any future compatibility guarantees. I would check JSR-337 thoroughly if this was likely to be an issue.

在以前的Java版本中,规范声明提供的脚本引擎是JRE供应商的实现细节。我不知道Java 8是否对所提供的引擎有任何强制性要求,或者它是否会提供任何未来的兼容性保证。如果这可能是一个问题,我会彻底检查JSR-337。

#3


1  

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

readFully (-scripting mode only)

readFully(仅限脚本模式)

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

此函数读取作为字符串参数传入的文件的全部内容并将其发送到stdout,或者您可以将结果分配给变量。

readFully example:

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

jjs> readFully(“text.txt”)这是位于当前工作目录中的text.txt文件的内容。

#4


0  

readFully is enabled only in scripting mode. Nashorn docs

readFully仅在脚本模式下启用。 Nashorn文档

try this:

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