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.




Comments

  1. Bartosz MajsakNo Gravatar May 15th

    Comment Arrow

    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!!!


  2. KrsnaNo Gravatar July 6th

    Comment Arrow

    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


  3. shivNo Gravatar July 6th

    Comment Arrow

    @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


  4. KrsnaNo Gravatar July 6th

    Comment Arrow

    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!


  5. shivNo Gravatar July 6th

    Comment Arrow

    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.


  6. DraganNo Gravatar June 25th

    Comment Arrow

    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:


  7. DraganNo Gravatar June 25th

    Comment Arrow

    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)


  8. DenisNo Gravatar November 19th

    Comment Arrow

    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


  9. DenisNo Gravatar November 19th

    Comment Arrow

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


  10. EbithNo Gravatar January 31st

    Comment Arrow

    Excellent solution, was looking for this one. Thank you so much.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

shiv

This author has not yet written a description. Please give them some time to get acquainted with the site and surely they will write their masterpiece.