tags:

views:

823

answers:

5

I must to implement command : java -jar test.jar page.xml | mysql -u user -p base in ant. So i Have tried with this task:

<java jar="test.jar" fork="true">
  <arg line="page.xml | mysql -u user -p base"/>
</java>

But i have got en exception with pipe - "|" :

 java.lang.IllegalArgumentException: Input already set; can't set to |

So, that's the problem:)

A: 

When you run a java program from Ant, the input and out from the program are captured by the Ant runtime - you can't try and redirect them elsewhere using that pipe.

If you want to do that, you might have better luck with the exec task, although that might suffer from the same problem.

skaffman
+1  A: 

There you are actually running a java command.

You need to use Exec task http://ant.apache.org/manual/CoreTasks/exec.html but not sure if there also you can run piped commands or not. Give it a try.

Bhushan
+6  A: 

The pipe (|) can only be used in a shell script. You're passing it as an argument to the java process.

So you need to execute a shell script. You can do this by executing (say) bash -b and passing the above as a script (albeit inline - you could write a separate script file but it seems a bit of an overhead here)

  <exec executable="bash">
    <arg value="-b"/>
    <arg value="java -jar test.jar page.xml | mysql -u user -p base"/>
  </exec>
Brian Agnew
Thanks guys, it helps!
Le_Coeur
It helps, or it works?
Brian Agnew
wow, i just thought that it works, but not...I have got:No such file or directory
Le_Coeur
From what ? You may need to set your classpath (or similar - don't forget you're now spawning off a separate process)
Brian Agnew
Porblem was in <arg value="...">, it should be <arg line="...">But now it writes: /usr/bin/java: /usr/bin/java: cannot execute binary file
Le_Coeur
Yeah, <arg value="-b"/> doesn't work for me, but <arg value="-c"/> does. Of course, I'm executing a binary other than java, but still using the pipe.
Quinn Taylor
+1  A: 

Another solution would be to wrap the java -jar test.jar page.xml | mysql -u user -p base into a separate script and call it with simple <exec> task.

Grzegorz Oledzki
+1  A: 

I don't know if this was ever resolved, but I was having a similar problem which I solved by using the following:

<exec executable="bash">
    <arg value="-c"/>
    <arg line='"java -jar test.jar page.xml | mysql -u user -p base"'/>
</exec>

Just thought I would share.