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.

3
  • Which Operating System is the script running on? Commented Jun 11, 2012 at 13:35
  • @ChadNouis, Windows (XP and 7). But, if possible, I'd prefer a platform-independent solution. Commented Jun 11, 2012 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 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. Commented Dec 24, 2014 at 16:13

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.