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

北慕城南 提交于 2019-12-07 16:47:29

问题


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.


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/10977619/how-do-i-kill-a-runaway-java-process-started-by-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!