3

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.

Community
  • 1
  • 1
amaidment
  • 6,942
  • 5
  • 52
  • 88
  • Which Operating System is the script running on? – Chad Nouis Jun 11 '12 at 13:35
  • @ChadNouis, Windows (XP and 7). But, if possible, I'd prefer a platform-independent solution. – amaidment Jun 11 '12 at 14:22
  • Would it be possible for you to use a parallel element with a daemons element? I'm not too savvy on the functionality of either and I understand you aren't looking for parallel, but I ran across [this thread](http://stackoverflow.com/questions/2299432/stopping-a-parallel-java-task-with-ant) as well as your thread in search of a solution of stopping a server java process. It looks like daemons will safely kill the correct java process when ant is finished and I assume that means when ant is killed early as well. – mejdev Dec 24 '14 at 16:13

3 Answers3

2

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:

amaidment
  • 6,942
  • 5
  • 52
  • 88
1

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • There are a couple of reasons for using `fork="yes"`; first, I want to set VM parameters - e.g. `maxmemory="128M"`, and second, lots of exceptions get thrown when I start my process with `fork="no"` - e.g. ehcache and database connectivity - which fits with the "odd things go wrong when you run this task" in the [docs](http://ant.apache.org/manual/Tasks/java.html) – amaidment Jun 11 '12 at 10:08
0

A cross-platform solution for killing processes will involve a bit of effort. See Java tool/method to force-kill a child process.

For Windows XP and later, the following batch script may be helpful:

for /f "skip=1 usebackq" %%h in (`wmic process where "Name like 'java%%.exe' and CommandLine like '%%path.to.MyClass%%'" get ProcessId ^| findstr .`) do taskkill /F /T /PID %%h

This script uses the built-in WMIC command to find the Process ID of any running java*.exe process that has path.to.MyClass somewhere in the Command Line. If you need WMIC to be more specific in matching your particular Java process, play with the properties listed by WMIC's help mode:

wmic process get /?

taskkill will kill a process tree (/T) given the root Process ID (/PID).

Community
  • 1
  • 1
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28