Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Javascript Engine backwards compatibility

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?

like image 937
nikkatsa Avatar asked Mar 19 '14 10:03

nikkatsa


People also ask

Is Java 8 backwards compatible?

Backward CompatibilityJava versions are expected to be binary backwards-compatible. For example, JDK 8 can run code compiled by JDK 7 or JDK 6. It is common to see applications leverage this backwards compatibility by using components built by different Java version.

What is the substitute of Renault JavaScript engine in Java 8?

Nashorn, pronounced "nass-horn," is German for "rhinoceros," and it's one of the animal names for a German tank destroyer used in World War II. It's also the name of the replacement -- introduced with Java 8 -- for the old, slow Rhino JavaScript engine.

Which JavaScript is replaced with Nashorn?

With Java 8, Nashorn, a much improved javascript engine is introduced, to replace the existing Rhino. Nashorn provides 2 to 10 times better performance, as it directly compiles the code in memory and passes the bytecode to JVM.

Why was Nashorn removed?

With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards. Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.


1 Answers

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.

like image 70
wickund Avatar answered Sep 19 '22 08:09

wickund