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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

According to this post : (bugs.openjdk.java.net/browse/…) 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! }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.