简体   繁体   中英

In Java how to programatically kill process on a specified port in Windows

Basically I am automation an application and prerequisite of my automation is that I am using several 3rd party libs which starts certain processes. At the end of the code execution, I want to close all such processes. The only thing I know is that these process runs on specific port always (for example 61120).

As part of clean up, I want to close the process running on port 61120 programatically in Java.

You could use netstat -a -b to get the executables and the port numbers. After that you can taskkill the executables by name. But i never would use this on a productive machine because it's some kind of ...ahm... dangerous.

Better to start processes by java ( Runtime.getRuntime().execute(...) ) and end them if you do not need them anymore.

Through this you can get the desired processes and port numbers netstat -a -b

Then there is a little API providing the desired functionality to operate with the processes:

https://java.net/projects/winp

Windows Process Library

try netstat -a -o -n Search for the port you need. If the console output is more then re-direct it to some file like: netstat -a -o -n > {drive}:/netlog.log search port in that log file.

taskkill /F /PID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM