27

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

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>

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

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.

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

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
nikkatsa
  • 1,751
  • 4
  • 26
  • 43

1 Answers1

48

One approach is to include

load("nashorn:mozilla_compat.js");

which supplies importClass.

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;

This is backward compatible with Rhino.

wickund
  • 1,077
  • 9
  • 13
  • 4
    According to this post : (https://bugs.openjdk.java.net/browse/JDK-8025132?focusedCommentId=13569345&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13569345) You can put the loading statement into a Try Catch block. This did the trick for me try { load("nashorn:mozilla_compat.js"); } catch (e) { // ignore the exception - perhaps we are running on Rhino! } – Steven Jun 30 '15 at 13:52