Bug 68542 - How to run external javascript with Java 17 using ant
Summary: How to run external javascript with Java 17 using ant
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.10.14
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-25 15:31 UTC by Yury
Modified: 2024-03-18 07:39 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yury 2024-01-25 15:31:52 UTC
Could you please explain, how to use
<script language="javascript" ...
with Java 17.
Under the https://ant.apache.org/manual/Tasks/script.html there's only information about using external dependencies.
Found one useful link - https://stackoverflow.com/questions/69832933/apache-ant-with-graal-vm
Seems like adding libs to %ANT%/lib helps but it's not the solution in our case as we need to run ant with both Java 8 and 17.
Adding
  <path id="javax.classpath">
    <pathelement location="./nashorn-core-15.4.jar"/>
    <pathelement location="./asm-9.6.jar"/>
    <pathelement location="./asm-util-9.6.jar"/>
  </path>
or
  <path id="javax.classpath">
    <pathelement location="./graal-sdk-22.3.1.jar"/>
    <pathelement location="./icu4j-72.1.jar"/>
    <pathelement location="./js-22.3.1.jar"/>
    <pathelement location="./js-launcher-22.3.1.jar"/>
    <pathelement location="./regex-22.3.1.jar"/>
    <pathelement location="./truffle-api-22.3.1.jar"/>
  </path>
 
and running 
    <script language="javascript" manager="javax" classpathref="javax.classpath">
...
Still generates
Java 15 has removed Nashorn, you must provide an engine for running JavaScript yourself. GraalVM JavaScript currently is the preferred option.
BUILD FAILED
The following error occurred while executing this line:
Unable to create javax script engine for javascript
Comment 1 Jaikiran Pai 2024-03-16 13:16:40 UTC
Hello Yury,

Does updating your javax.classpath definition to:

  <path id="javax.classpath">
    <pathelement location="./graal-sdk-22.3.1.jar"/>
    <pathelement location="./icu4j-71.1.jar"/>
    <pathelement location="./js-22.3.1.jar"/>
    <pathelement location="./js-scriptengine-22.3.1.jar"/>
    <pathelement location="./regex-22.3.1.jar"/>
    <pathelement location="./truffle-api-22.3.1.jar"/>
  </path>


get it working? Notice that I've used js-scriptengine (instead of js-launcher) and also used icu4j-71.1 instead of icu4j-72.1 from your example.

This following build file works fine for me (make sure you do have those exact jars locally):

<project name="squares" default="run-squares-test">

  <path id="javax.classpath">
    <pathelement location="./graal-sdk-22.3.1.jar"/>
    <pathelement location="./icu4j-71.1.jar"/>
    <pathelement location="./js-22.3.1.jar"/>
    <pathelement location="./js-scriptengine-22.3.1.jar"/>
    <pathelement location="./regex-22.3.1.jar"/>
    <pathelement location="./truffle-api-22.3.1.jar"/>
  </path>


  <target name="run-squares-test">
    <script language="javascript" manager="javax" classpathref="javax.classpath"> <![CDATA[
      for (i = 1; i <= 10; i++) {
        echo = squares.createTask("echo");
        echo.setMessage(i*i);
        echo.perform();
      }
    ]]> </script>
  </target>
</project>

Running "ant" on this gives:

run-squares-test:
   [script] [To redirect Truffle log output to a file use one of the following options:
   [script] * '--log.file=<path>' if the option is passed using a guest language launcher.
   [script] * '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher.
   [script] * Configure logging using the polyglot embedding API.]
   [script] [engine] WARNING: The polyglot context is using an implementation that does not support runtime compilation.
   [script] The guest application code will therefore be executed in interpreted mode only.
   [script] Execution only in interpreted mode will strongly impact the guest application performance.
   [script] For more information on using GraalVM see https://www.graalvm.org/java/quickstart/.
   [script] To disable this warning the '--engine.WarnInterpreterOnly=false' option or use the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property.
     [echo] 1
     [echo] 4
     [echo] 9
     [echo] 16
     [echo] 25
     [echo] 36
     [echo] 49
     [echo] 64
     [echo] 81
     [echo] 100


If this works for you then, I'll update our manual to use the newer co-ordinates for these dependencies and include an example to use it.
Comment 2 Yury 2024-03-18 07:39:54 UTC
Thanks, looks like now it works.