Search code examples
javascriptjavaantnashornjava-16

java.lang.IllegalAccessError trying to use embedded javascript with a standalone nashorn.jar file and Java 16+29 (or higher)


We use embedded javascript code in some of our ANT build files with the Nashorn functionality built into Java 8+. In Java 15, the Nashorn functionality was removed, so we tested this out with a standalone nashorn.jar file. We just added:

<classpath> <pathelement location="<path>/nashorn.jar"/> </classpath>

and things ran just fine. And with Java 16, this was working fine with build 16+27.

Starting at java 16+29 (and above) when I run a simple test ANT script:

<project name="Test" default="build" basedir=".">
    <target name="build">
      <echo message="Begin"/>
      <script language="javascript">
        <classpath> <pathelement location="C:/nashorn/nashorn-0.1.0.2013070801.jar"/> </classpath>
        print('Hello from embedded Javascript');
      </script>
      <echo message="End"/>
    </target>
</project>

I get the following error:

build.xml:5: java.lang.IllegalAccessError: class jdk.nashorn.internal.codegen.types.Type (in unnamed module @0xeafc191) cannot access class jdk.internal.org.objectweb.asm.Type (in module java.base) because module java.base does not export jdk.internal.org.objectweb.asm to unnamed module @0xeafc191
        at jdk.nashorn.internal.codegen.types.Type.getInternalName(Type.java:282)
        at jdk.nashorn.internal.codegen.CompilerConstants.className(CompilerConstants.java:243)
        at jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup(CompilerConstants.java:372)
        at jdk.nashorn.internal.codegen.types.BooleanType.<clinit>(BooleanType.java:70)
        at jdk.nashorn.internal.codegen.types.Type.<clinit>(Type.java:669)
        at jdk.nashorn.internal.codegen.CompilerConstants.className(CompilerConstants.java:243)
        at jdk.nashorn.internal.codegen.CompilerConstants.virtualCall(CompilerConstants.java:551)
        at jdk.nashorn.internal.codegen.CompilerConstants.virtualCall(CompilerConstants.java:536)
        at jdk.nashorn.internal.runtime.ScriptObject.<clinit>(ScriptObject.java:141)
        at jdk.nashorn.internal.runtime.Context.newGlobalTrusted(Context.java:819)
        at jdk.nashorn.internal.runtime.Context.newGlobal(Context.java:636)
        at jdk.nashorn.api.scripting.NashornScriptEngine$2.run(NashornScriptEngine.java:275)
        at jdk.nashorn.api.scripting.NashornScriptEngine$2.run(NashornScriptEngine.java:271)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
        at jdk.nashorn.api.scripting.NashornScriptEngine.createNashornGlobal(NashornScriptEngine.java:271)
        at jdk.nashorn.api.scripting.NashornScriptEngine.<init>(NashornScriptEngine.java:108)
        at jdk.nashorn.api.scripting.NashornScriptEngine.<init>(NashornScriptEngine.java:82)
        at jdk.nashorn.api.scripting.NashornScriptEngineFactory.getScriptEngine(NashornScriptEngineFactory.java:139)
        at java.scripting/javax.script.ScriptEngineManager.getEngineByName(ScriptEngineManager.java:241)
        at org.apache.tools.ant.util.optional.JavaxScriptRunner.createEngine(JavaxScriptRunner.java:193)
        at org.apache.tools.ant.util.optional.JavaxScriptRunner.evaluateScript(JavaxScriptRunner.java:141)
        at org.apache.tools.ant.util.optional.JavaxScriptRunner.executeScript(JavaxScriptRunner.java:82)
        at org.apache.tools.ant.taskdefs.optional.Script.execute(Script.java:53)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
        at org.apache.tools.ant.Task.perform(Task.java:350)
        at org.apache.tools.ant.Target.execute(Target.java:449)
        at org.apache.tools.ant.Target.performTasks(Target.java:470)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1391)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1254)
        at org.apache.tools.ant.Main.runBuild(Main.java:830)
        at org.apache.tools.ant.Main.startAnt(Main.java:223)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:284)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101)

When using ANT 1.8.4 and ANT 1.10.5.

So my questions are: Am I doing something wrong, or is this a bug in later builds of Java 16 that I should report? Any suggested workarounds?


Solution

  • Standalone Nashorn classes live in the org.openjdk.nashorn package. The pre-Java-15 Nashorn classes live in the jdk.nashorn package, so you're picking up those classes that shipped with Java 14 or earlier.

    I see you're actually using some god-knows-what version of Nashorn with 2013 in its name hosted on Clojars? I don't even know what that is, I presume that's some version of the JDK built-in classes repackaged as a standalone library.

    To get the currently supported standalone Nashorn, go to https://github.com/openjdk/nashorn/#getting-started for the Maven Central link and other resources. Give it a try and let me know if it worked. Note that it has some JAR dependencies that you'll need to add as well (specifically, ASM.)