Java 8 Javascript 引擎向后兼容性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22503100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:06:30  来源:igfitidea点击:

Java 8 Javascript Engine backwards compatibility

javaantjava-8nashorn

提问by nikkatsa

I am trying out Java 8 in my project and I am stuck in an error related to my build process.

我正在我的项目中试用 Java 8,但遇到了与我的构建过程相关的错误。

I am using ANT scripts and at some point i am using some javascript (embeded into ANT) to do some build specific operations. The part of the script that is causing the error looks like below:

我正在使用 ANT 脚本,并且在某些时候我正在使用一些 javascript(嵌入到 ANT 中)来执行一些特定于构建的操作。导致错误的脚本部分如下所示:

<script language="javascript"> 

        <![CDATA[

        importClass(java.io.File);
        importClass(java.io.FileReader);
                    ...
                    ]]>
</script>

The project is building fine with Java 7 or Java 6, but it gives me some errors when i am using Java 8. These errors are related to the upgrade of the JS engine.

该项目使用 Java 7 或 Java 6 构建得很好,但是当我使用 Java 8 时它给了我一些错误。这些错误与 JS 引擎的升级有关。

In particular i am getting the following exception:

特别是我收到以下异常:

javax.script.ScriptException: ReferenceError: "importClass" is not defined in at line

javax.script.ScriptException: ReferenceError: "importClass" is not defined in at line

After some googling i found out that it is related to the below issue in the JDK

经过一番谷歌搜索后,我发现它与 JDK 中的以下问题有关

[#JDK-8025132]

[#JDK-8025132]

I tried what is suggested in the comments but without luck.

我尝试了评论中的建议,但没有运气。

How can I make Java 8 Nashorn engine to be compatible with the Rhino JS engine?

如何使 Java 8 Nashorn 引擎与 Rhino JS 引擎兼容?

采纳答案by wickund

One approach is to include

一种方法是包括

load("nashorn:mozilla_compat.js");

which supplies importClass.

它提供 importClass。

On the other hand, you can use java.io.File, java.io.FileReader, ... directly without importing.

另一方面,您可以直接使用 java.io.File, java.io.FileReader, ... 无需导入。

var File = java.io.File;
var FileReader = java.io.FileReader;

This is backward compatible with Rhino.

这与 Rhino 向后兼容。