Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I kill a runaway Java process started by Ant?

Tags:

java

kill

ant

If I start a forked java process from an ant script and kill the ant process, it does not kill the java process. This is the case whether running it from the IDE or from the command line.

<target name="myTarget" >
  <java classname="path.to.MyClass" 
        fork="yes" 
        failonerror="true" 
        maxmemory="128M">
    <classpath refid="run" />
  </java>
</target>

Is there a way to link these, so that killing the ant process will kill the java process?

I've seen the following Q&A - but this seems to focus on how to kill the java process manually. I don't want to do this, because I have a number of other java applications running, and finding the right java.exe process to kill in TaskManager is not always straight forward.

like image 421
amaidment Avatar asked Feb 04 '26 08:02

amaidment


2 Answers

Unfortunately, it appears that this is a long-standing and known issue.

When the Ant task is terminated, the forked Java process shutdown hooks are not fired. (This seems to have been an issue since Java 1.4 (!))

For reference:

  • an Ant bug report
  • a Java not-a-bug report
like image 79
amaidment Avatar answered Feb 05 '26 22:02

amaidment


If you set fork to "no", the same VM will be used, so killing the ant process will kill this specific java process too.

like image 23
Denys Séguret Avatar answered Feb 05 '26 21:02

Denys Séguret