user4316868
user4316868

Reputation:

Java Nashorn compatibility file crashes

When I try to load the Nashorn compatibility file for Rhino (load("nashorn:mozilla_compat.js")) it comes up with the following error:

java.lang.RuntimeException: javax.script.ScriptException: ReferenceError: "net" is not defined in nashorn:mozilla_compat.js at line number 67

I've tried everything to get it to work but nothing has helped :(

Upvotes: 1

Views: 1377

Answers (2)

fc7
fc7

Reputation: 125

This can happen if your script (not mozilla_compat.js itself) contains a declaration with a qualified name like this:

var x = new net.yourdomain.yourpackage.ClassName();

instead of doing

importPackage(Packages.net.yourdomain.yourpackage);

var x = new ClassName();

The former works in Rhino, but not in Nashorn, even with the compatibility script. The latter however will work in both environments.

Upvotes: 2

A. Sundararajan
A. Sundararajan

Reputation: 4405

I ran the following code with the latest JDK 8 update released (8u60) - available for download @ http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

import javax.script.*;

public class Main {
   public static void main(String[] ar) throws Exception {
      ScriptEngineManager m = new ScriptEngineManager();
      ScriptEngine e = m.getEngineByName("nashorn");
      e.eval("load('nashorn:mozilla_compat.js')");
      // this should print 'function' and mozilla_compat.js defines that function
      e.eval("print(typeof importClass)");    
   }
}

And it printed "function" as expected. I checked it on jdk9-dev tip forest build as well. It works with that version as well. Will you please print "java -version" and make sure you're using recent JDK 8 ?

Upvotes: 0

Related Questions