linux关掉java,java 如何关闭服务进程(win+linux) ?

cn.hutool.core.net.NetUtil#isUsableLocalPort(port)isUsableLocalPort(int port)

检测本地端口可用性

来自org.springframework.util.SocketUtils

上面方法如果不想引入jar:public static boolean isUsableLocalPort(int port) {

// issue#765@Github, 某些绑定非127.0.0.1的端口无法被检测到

try (ServerSocket ss = new ServerSocket(port)) {

ss.setReuseAddress(true);

} catch (IOException ignored) {

return false;

}

try (DatagramSocket ds = new DatagramSocket(port)) {

ds.setReuseAddress(true);

} catch (IOException ignored) {

return false;

}

return true;

}public static void main(String[] args) throws IOException, InterruptedException {

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/C", "netstat -n -o | findstr :12345");

Process process = builder.start();

process.waitFor();

printProcessStreamAndKill(process.getInputStream());

}

private static void printProcessStream(InputStream inputStream) throws IOException {

int bytesRead = -1;

byte[] bytes = new byte[1024];

String output = "";

while ((bytesRead = inputStream.read(bytes)) > -1) {

output = output + new String(bytes, 0, bytesRead);

}

System.out.println(" The netstat command response is \r\n" + output);

}

public static void killProcess(String pid) throws Exception {

Runtime.getRuntime().exec(KILL + pid);

}

linux 同样的方式,命令为查找端口占用进程:lsof -i:12345

关闭进程:kill -9 ${pid}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值