Java 8 Javascript 引擎向后兼容性 [英] Java 8 Javascript Engine backwards compatibility

查看:44
本文介绍了Java 8 Javascript 引擎向后兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

<script language="javascript"> 

        <![CDATA[

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

该项目使用 Java 7 或 Java 6 构建良好,但是当我使用 Java 8 时出现一些错误.这些错误与 JS 引擎的升级有关.

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.

特别是我收到以下异常:

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

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

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

[#JDK-8025132]

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

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

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

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

推荐答案

一种方法是包含

load("nashorn:mozilla_compat.js");

提供 importClass.

which supplies importClass.

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

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

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

这与 Rhino 向后兼容.

This is backward compatible with Rhino.

这篇关于Java 8 Javascript 引擎向后兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆