Java 8 Javascript Engine backwards compatibility
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.
Related videos on Youtube
nikkatsa
Updated on June 17, 2022Comments
-
nikkatsa almost 2 years
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
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?
-
Philipp about 10 yearsRelated to stackoverflow.com/questions/22502630/…
-
-
Steven almost 9 yearsAccording 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! }