1

I'm trying to kill the running port(8080) by using the below java code. But, it seems the code is not working.

Process process = Runtime.getRuntime().exec("netstat -aon | find 8080");
InputStream ips = process.getInputStream();
InputStreamReader isr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(isr);
String value = null;
while ((value = br.readLine()) != null) {
    System.out.println(value);
}

I didn't find that anything got printed into output console. And not sure how to proceed further.

I want this code to work on windows and linux box. Is there any general solution to make this code work on both OS and how to kill the port using JAVA ? Any leads?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • You cannot kill a port. You can kill the application using a given port. – Gaurav May 28 '18 at 05:35
  • 1
    Refer this answer for making it work on windows and linux: https://stackoverflow.com/a/6356423/4358787 – Gaurav May 28 '18 at 05:38
  • Is there any other way to get the pid for the port number?( TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 21448) by the way I can try for killing it explicitly.. – ArrchanaMohan May 28 '18 at 05:39
  • You can try to kill it by executing shutdown.bat file of your server. – Adya May 28 '18 at 05:45
  • @ArrchanaMohan There is no java API to find PID using a port. You need to call windows-native netstat command only to get the PID.\ – Gaurav May 28 '18 at 05:55

0 Answers0