4

I am having issues using the <script> tag in Ant and I am hoping someone can help. I want to use JavaScript in my Ant build.xml. Something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project name="java" default="main" basedir=".">
  <target name="main">
    <script language="javascript"> <![CDATA[
 println("hello, world")
    ]]> </script>
  </target>
</project>*

Unfortunately this only displays and error:

build.xml:4: Could not create task or type of type: script.

I have located the required jar file (js.jar) for this to work and moved it to ANT_HOME/lib, but I am still stuck as of how to get this to work.

4
  • Which version of Ant are you using? Run "ant -version" on the command line. Also, which OS are you running? Commented Apr 16, 2013 at 15:53
  • ANT is version 1.6.5 on a red hat 5 u 8 OS Commented Apr 18, 2013 at 7:38
  • What is your java version ? See my comment on the answer of Chad Commented Apr 18, 2013 at 19:20
  • also 2 problems => missing ';' after println.. and trailing '*' after closing project tag Commented Apr 18, 2013 at 19:34

3 Answers 3

3

In addition to js.jar, you need to add bsf.jar and commons-logging-*.jar to ANT_HOME/lib. In your Ant distribution, there is a file named docs/manual/install.html. The Library Dependencies section of this HTML file documents where you can download these files.

println isn't supported in JavaScript. Instead, use the following:

<project name="jsTest" default="main">
  <target name="main">
    <script language="javascript"> <![CDATA[
        var echo = jsTest.createTask("echo");
        echo.setMessage("hello, world");
        echo.perform();
    ]]> </script>
  </target>
</project>
Sign up to request clarification or add additional context in comments.

4 Comments

you don't need js.jar and bsf.jar anymore, as jdk >= 6 ships with a builtin Javascript Scripting Engine based on Rhino 1.6R2
why not simply use print("hello, world"); instead ?
Ant 1.6.5 doesn't work with JDK 1.6's JavaScript engine. If user2286073 upgrades to Ant 1.7 or later, then all of these issues will be resolved.
you're are right, using ant 165 will produce java.lang.NoClassDefFoundError: org/apache/bsf/BSFException
3

You can also instantiate and use Java classes in Javascript as available via Rhino(JRE pre 1.8) or Nashorn(JRE 1.8+) when you need to.

<script language="javascript">
  with(new JavaImporter(java.lang, java.io)){
    System.out.println("hello, world");// <--!!!
  }
</script>

You may create and use JavaScript functions of your own.

<script language="javascript">
  with(new JavaImporter(java.lang, java.io)){
    var fun = function(a,b){
      System.out.println(a+b);
    };
    fun(1,2);
  }
</script>

Code above prints

3.0 

Loops, recursion and everything you've been dreaming about... except strong type checking:-)

Comments

1

Beside the two syntax errors - missing ';' after println.. and trailing '*' after closing project tag - you should upgrade your ant installation to a version >= 1.7.x in conjunction with jdk >= 6 to be able to use the builtin javascript engine.
When using jdk >=6 the use of println is no problem, see :

import javax.script.*;
public class ExecuteJS {
 public static void main(String[] args) throws Exception {
 ScriptEngineManager factory = new ScriptEngineManager();
 ScriptEngine engine = factory.getEngineByName("JavaScript");
 engine.eval("print('Line1')");
 engine.eval("println('Line2')");
 engine.eval("print('Line3')");
 engine.eval("println('Line4')");
 }
}

output :

Line1Line2
Line3Line4

and

<project>
 <script language="javascript">
  println("hello, world");
 </script>
</project>

But further testing with Ant 1.9.0 / Win7 (my linux box is down right now) / jdk1.7.0_21 revealed some oddities :

<project>
 <script language="javascript">
  println("hello, world");
 </script>
</project>

works

<project default="foo">
<target name="foo"> 
 <script language="javascript">
  println("hello, world");
 </script>
</target>
</project>

works also

<project name="whatever" default="foo">
<target name="foo"> 
 <script language="javascript">
  println("hello, world");
 </script>
</target>
</project>

works also, whereas

<project name="java" default="foo">
<target name="foo"> 
 <script language="javascript">
  println("hello, world");
 </script>
</target>
</project>

results in

BUILD FAILED
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot read property "PrintWriter" from undefined (print#8) in print at line number 8

Strange !?
Seems like a bug, so finally upgrade ant >= 1.7.x and jdk >= 1.6 and
don't use 'java' in name attribute of project :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.