Killing Java Process from Ant in Windows
I wanted to write a ant script for one of the environment, which would perform following tasks in order which will be repeated
1. Take Latest Code
2. Compile
3. Create WAR
4. Undeploy currently installed WAR from tomcat
5. Stop the tomcat
6. Copy the WAR to tomcat webapps folder
7. Start the tomcat
Starting the tomcat -
<target name="tomcat.start">
<exec executable="${tomcat.home}/bin/startup.bat" spawn="true">
<env key="CATALINA_HOME" value="${tomcat.home}"/>
</exec>
<sleep seconds="10"/>
</target>
Stopping the tomcat -
I was facing some issues using the tomcat shutdown.bat as tomcat server was not stopping due to one of the application deployed was not getting stopped gracefully. And the next time tomcat tried to start it used to give “JVM_BIND Exception -Port already in use”.
As Windows doesn’t have a native kill command, I have used following which allowed me to kill any java process using ant.
PsKill – Running PsKill with a process ID directs it to kill the process of that ID on the local computer.
jps – The jps command will report the process ID of each JVM running on local computer.
<target name="tomcat.stop">
<exec executable="${env.JAVA_HOME}/bin/jps" output="pid.out.file"/>
<loadfile srcfile="pid.out.file" property="pid.out">
<filterchain>
<linecontains>
<contains value="Bootstrap"/>
</linecontains>
<tokenfilter>
<deletecharacters chars="Bootstrap"/>
<trim/>
<ignoreblank/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadfile>
<echo>Killing tomcat instance with PID - "${pid.out}"</echo>
<exec executable="pskill">
<arg value="${pid.out}"/>
</exec>
<delete file="pid.out.file"/>
</target>
The above way can be used to kill any java process using the ant.





Bartosz Majsak
May 15th
I was trying to test my web service client using soapUI mockservice runner fired by ANT before TestNG suite and your solution is working perfectly fine! I saved a lot of time. Thanks a lot!!!
Krsna
July 6th
Can you please suggest how to run the same for firefox?
as i want to kill a particular firefox tab not all.
Thanks in advance!
–Krsna
shiv
July 6th
@Krsna : Since firefox runs as a single process, you can’t kill individual tab. Mozilla though have planned to implement it for future version. More at – https://wiki.mozilla.org/Content_Processes
Krsna
July 6th
Thanks shiv for your reply.
But can we do it for IE?
as i have a client requirement and after executing some job in browser i want to close it. IE would also work.
Awaiting your reply!
shiv
July 6th
In IE also till IE8 [http://blogs.msdn.com/ie/archive/2008/03/11/ie8-and-loosely-coupled-ie-lcie.aspx] you can’t kill individual tabs. IE8 still I am not sure since I have not tried it out.
If you want to kill the entire browser session, it should be similar to the way I had done above. Instead of “jps” you can use “tasklist” command which comes with Windows to find the PID of a process and then use “pskill” to kill it.
Dragan
June 25th
I’ve bumped to this blog entry and I would like to say thanks and to add the following: If you want to avoid pskill installation you can use taskkill command (change lines 17-19 to:
Dragan
June 25th
Ups, my code was stripped out. It should say (change regular brackets with less than and greater than):
(exec executable=”taskkill”)
(arg value=”/pid” /)
(arg value=”$CURLY_OPENpid.outCURLY_CLOSED” /)
(/exec)
Denis
November 19th
Good idea!
But if on one computer started many tomcats I’m using
jps.exe -mlvV
and parse result
var token = self.getToken();
var spacePos = token.indexOf(” “);
var tomcatDir = project.getProperty(“tomcat.dir”);
// preparing to single format
tomcatDir = tomcatDir.replace(“/”, “\\”);
token = token.replace(“/”, “\\”);
//print(“tomcatDir: ” + tomcatDir);
//print(“token: ” + token);
if (spacePos != -1) {
if (token.contains(tomcatDir)) {
//print(“spacePos: ” + spacePos);
var pid = token.substring(0, spacePos);
print(“pid for ‘” + tomcatDir + “‘: ” + pid);
self.setToken(pid);
} else {
self.setToken(null);
}
} else {
self.setToken(null);
}
Killing Tomcat (${tomcat.dir}) instance with PID – “${pid.out}”
where ant property
tomcat.dir=c:/apps/apache-tomcat-6.0.29
Denis
November 19th
<target name=”tomcat.stop”>
<touch file=”${tomcat.pid.tmp.file}”/>
<!–print java processes–>
<exec executable=”jps.exe” dir=”${env.JAVA_HOME}/bin” output=”${tomcat.pid.tmp.file}”>
<arg line=”-mlvV”/>
</exec>
<!–getting single for project Tomcat Process ID–>
<loadfile srcfile=”${tomcat.pid.tmp.file}” property=”pid.out”>
<filterchain>
<linecontains>
<contains value=”Bootstrap”/>
</linecontains>
<tokenfilter>
<scriptfilter language=”javascript”>
var token = self.getToken();
var spacePos = token.indexOf(” “);
var tomcatDir = project.getProperty(“tomcat.dir”);
// preparing to single format
tomcatDir = tomcatDir.replace(“/”, “\\”);
token = token.replace(“/”, “\\”);
//print(“tomcatDir: ” + tomcatDir);
//print(“token: ” + token);
if (spacePos != -1) {
if (token.contains(tomcatDir)) {
//print(“spacePos: ” + spacePos);
var pid = token.substring(0, spacePos);
print(“pid for ‘” + tomcatDir + “‘: ” + pid);
self.setToken(pid);
} else {
self.setToken(null);
}
} else {
self.setToken(null);
}
</scriptfilter>
<trim/>
<ignoreblank/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadfile>
<echo>Killing Tomcat (${tomcat.dir}) instance with PID – “${pid.out}”</echo>
<exec executable=”${pskill.path}/pskill.exe” spawn=”true” dir=”${pskill.path}”>
<arg value=”${pid.out}”/>
</exec>
</target>
Ebith
January 31st
Excellent solution, was looking for this one. Thank you so much.
Add Yours
YOU