Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8025132

ScriptEngine throws exception when evaluating importClass and importPackage

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 8
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b106)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b48, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      New scripting engine introduced in Java 8 no longer seems to handle importClass or importPackage built-in functions. When referenced in a scripting, it throws javax.script.ScriptException: ReferenceError: "importClass" is not defined in <eval> at line number ...

      REGRESSION. Last worked in version 7u21

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the source code specified in this bug report


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      SEVERE: null
      javax.script.ScriptException: ReferenceError: "importClass" is not defined in <eval> at line number 1
      at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:553)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:537)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:517)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:513)
      at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:202)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
      at java8scripting.Java8Scripting.main(Java8Scripting.java:15)
      Caused by: <eval>:1 ReferenceError: "importClass" is not defined
      at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
      at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
      at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
      at jdk.nashorn.api.scripting.NashornScriptEngine.__noSuchProperty__(NashornScriptEngine.java:286)
      at jdk.nashorn.internal.scripts.Script$engine._L35(nashorn:engine/resources/engine.js:37)
      at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
      at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:527)
      at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:204)
      at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:395)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:535)
      ... 5 more

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package java8scripting;

      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;

      public class Java8Scripting {
          public static void main(String[] args)
          {
              try
              {
                  ScriptEngineManager factory = new ScriptEngineManager();
                  ScriptEngine engine = factory.getEngineByName("JavaScript") ;
                  engine.eval("importClass(java.util.Vector)
      " +
                          "print(\"Hello world\") ;");
              }
              catch (Exception ex)
              {
                  Logger.getLogger(Java8Scripting.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      }

      ---------- END SOURCE ----------

        Activity

        Hide
        sundar Sundararajan Athijegannathan added a comment -
        Nashorn is not a drop-in replacement JavaScript implementation for Rhino. Nashorn implements ECMAScript 262 Edition 5.1 specification with few compatible extensions with other JS implementations. importClass, importPackage etc.

        You need to evaluate "load('nashorn:mozilla_compat.js')" to enable rhino compatible top-level functions and object methods (of few objects). Please note that while importClass and importPackage is enabled by that load call, not every feature of Rhino is supported. Also, preferred way to refer to Java types is "Java.type" (of top level "Java" property).

        See also http://hg.openjdk.java.net/nashorn/jdk8/nashorn/raw-file/29b2b2ed954c/docs/JavaScriptingProgrammersGuide.html

        Show
        sundar Sundararajan Athijegannathan added a comment - Nashorn is not a drop-in replacement JavaScript implementation for Rhino. Nashorn implements ECMAScript 262 Edition 5.1 specification with few compatible extensions with other JS implementations. importClass, importPackage etc. You need to evaluate "load('nashorn:mozilla_compat.js')" to enable rhino compatible top-level functions and object methods (of few objects). Please note that while importClass and importPackage is enabled by that load call, not every feature of Rhino is supported. Also, preferred way to refer to Java types is "Java.type" (of top level "Java" property). See also http://hg.openjdk.java.net/nashorn/jdk8/nashorn/raw-file/29b2b2ed954c/docs/JavaScriptingProgrammersGuide.html
        Hide
        sundar Sundararajan Athijegannathan added a comment -
        How to make a script that uses importClass and importPackage work on JDK 6, 7 and JDK 8?

        * nashorn defines these rhino compatibility functions in a built-in script file - which can be loaded into any script like:

        load("nashorn:mozilla_compat.js");

        importClass(java.util.ArrayList);
        var v = new ArrayList();

        * if you want to run the same script on Rhino as well as Nashorn, you can do something like this:

        try {
            load("nashorn:mozilla_compat.js");
        } catch (e) {
           // ignore the exception - perhaps we are running on Rhino!
        }

        importClass(java.util.ArrayList);
        var v = new ArrayList();

        If you don't prefer to catch-n-ignore hack, you can check for JDK version via System property and then load

        var version = java.lang.System.getProperty("java.version");
        if (version.startsWith("1.8.0")) {
            load("nashorn:mozilla_compat.js");
        }
        importClass(java.util.ArrayList);
        var v = new ArrayList();

        Either should work fine.
        Show
        sundar Sundararajan Athijegannathan added a comment - How to make a script that uses importClass and importPackage work on JDK 6, 7 and JDK 8? * nashorn defines these rhino compatibility functions in a built-in script file - which can be loaded into any script like: load("nashorn:mozilla_compat.js"); importClass(java.util.ArrayList); var v = new ArrayList(); * if you want to run the same script on Rhino as well as Nashorn, you can do something like this: try {     load("nashorn:mozilla_compat.js"); } catch (e) {    // ignore the exception - perhaps we are running on Rhino! } importClass(java.util.ArrayList); var v = new ArrayList(); If you don't prefer to catch-n-ignore hack, you can check for JDK version via System property and then load var version = java.lang.System.getProperty("java.version"); if (version.startsWith("1.8.0")) {     load("nashorn:mozilla_compat.js"); } importClass(java.util.ArrayList); var v = new ArrayList(); Either should work fine.

          People

          • Assignee:
            ndcosta Nelson Dcosta
            Reporter:
            ndcosta Nelson Dcosta
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: