Reputation: 7268
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.
Upvotes: 3
Views: 5198
Reputation: 7268
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:
Upvotes: 2
Reputation: 7041
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).
Upvotes: 0
Reputation: 382167
If you set fork
to "no", the same VM will be used, so killing the ant process will kill this specific java process too.
Upvotes: 1