Matthew Stern
Matthew Stern

Reputation: 75

Nashorn not recognized in Ant script with Amazon Corretto 21 and DITA-OT 4.2.3

I'm migrating our custom plugins to DITA OT 4.2.3, which requires a newer version of the JDK. We're using Amazon Corretto 21.0.3.9.1. We have one Ant build script that contains JavaScript. Since the JDK no longer contains the JavaScript engine, we're using Nashorn 15.4.

I copied the latest versions of the files (nashorn-core-15.4.jar, asm-9.7.jar, and asm-util-9.7.jar) into the lib folder of the plugin. In the Ant script, I added the following lines between <project> and the first <target>.

<path id="javax.classpath">
    <pathelement location="./lib/nashorn-core-15.4.jar"/>
    <pathelement location="./lib/asm-9.7.jar"/>
    <pathelement location="./lib/asm-util-9.7.jar"/>
</path>

I changed the <script> element as follows:

<script language="javascript" manager="javax" classpathref="javax.classpath">
      <![CDATA[var inputFolderInitVar = project.getProperty("inputFolderInit");
      var lastBackSlash = inputFolderInitVar.lastIndexOf("\\");
      project.setProperty("inputFolder", inputFolderInit.substring(0, lastBackSlash));]]>
</script>

But I'm still getting the "Unable to create javax script engine for javascript" error. Any idea what I'm missing?

FYI, I'm looking at regular expressions with ant-contrib or using Groovy script if the JavaScript option doesn't work. I would first like to see if we can get this working with JavaScript. Thanks.

Upvotes: 0

Views: 150

Answers (1)

Attila Szegedi
Attila Szegedi

Reputation: 4565

Hard to tell from this much, but have you tried using ASM 7.3.1 instead? Nashorn uses ASM 7.3.1 as its declared dependency. And have you also tried adding all of asm, asm-commons, asm-tree, and asm-util? These are declared in the POM as dependencies.

Here's a little reproducer that works for me (with all libraries in a top-level directory):

<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-test" default="script-test" basedir=".">
  <target name="script-test">
    <path id="javax.classpath">
      <pathelement location="nashorn-core-15.4.jar"/>
      <pathelement location="asm-7.3.1.jar"/>
      <pathelement location="asm-commons-7.3.1.jar"/>
      <pathelement location="asm-tree-7.3.1.jar"/>
      <pathelement location="asm-util-7.3.1.jar"/>
    </path>
    <script language="javascript" manager="javax" classpathref="javax.classpath">
      <![CDATA[project.setProperty("foo", "bar");]]>
    </script>
    <echo message="${foo}"/>
  </target>
</project>

And here's my java and ant versions and the result of running it:

» java -version
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)
» ant -version
Apache Ant(TM) version 1.10.14 compiled on August 16 2023
» ant
Buildfile: /Users/attila/Documents/projects/ant-test/build.xml

script-test:
     [echo] bar

BUILD SUCCESSFUL
Total time: 0 seconds
» 

So all I can say it works for me on Java 21.0.2 and Ant 1.10.14.

Upvotes: 1

Related Questions