You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中程序化终止占用指定端口的进程(跨平台)

在Java中通过监听端口终止进程(跨平台实现)

可以实现,但Java本身没有原生API直接通过端口终止进程,需要自己封装跨平台逻辑——先定位占用目标端口的进程ID(PID),再根据PID终止进程。

核心步骤

1. 定位占用端口的PID

根据操作系统执行不同系统命令,解析输出提取PID:

  • Windows:执行netstat -ano | findstr :<端口号>,输出最后一列即为PID
  • Linux/macOS:优先用lsof -i :<端口号> | grep LISTEN | awk '{print $2}';若系统无lsof,可替换为ss -tulpn | grep :<端口号> | awk '{split($7,a,","); print a[2]}'

2. 通过PID终止进程

按系统执行对应终止命令:

  • Windows:taskkill /F /PID <PID>(/F参数强制终止进程)
  • Linux/macOS:kill -9 <PID>(-9参数强制杀死进程)

代码示例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PortKiller {
    public static void killProcessByPort(int port) throws IOException, InterruptedException {
        String os = System.getProperty("os.name").toLowerCase();
        String pid = getPidByPort(port, os);
        
        if (pid == null || pid.isEmpty()) {
            System.out.println("端口 " + port + " 未被占用");
            return;
        }

        ProcessBuilder pb;
        if (os.contains("win")) {
            pb = new ProcessBuilder("cmd.exe", "/c", "taskkill", "/F", "/PID", pid);
        } else {
            pb = new ProcessBuilder("bash", "-c", "kill -9 " + pid);
        }
        
        Process process = pb.start();
        int exitCode = process.waitFor();
        if (exitCode == 0) {
            System.out.println("成功终止占用端口 " + port + " 的进程(PID:" + pid + ")");
        } else {
            System.out.println("终止进程失败,退出码:" + exitCode);
        }
    }

    private static String getPidByPort(int port, String os) throws IOException, InterruptedException {
        ProcessBuilder pb;
        if (os.contains("win")) {
            pb = new ProcessBuilder("cmd.exe", "/c", "netstat", "-ano", "|", "findstr", ":" + port);
        } else {
            // 兼容无lsof的Linux系统
            pb = new ProcessBuilder("bash", "-c", "command -v lsof >/dev/null 2>&1 && lsof -i :" + port + " | grep LISTEN | awk '{print $2}' || ss -tulpn | grep :" + port + " | awk '{split($7,a,\",\"); print a[2]}'");
        }

        Process process = pb.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        String pid = null;
        while ((line = reader.readLine()) != null) {
            if (os.contains("win")) {
                // 拆分Windows netstat输出,取最后一列的PID
                String[] parts = line.trim().split("\\s+");
                pid = parts[parts.length - 1];
            } else {
                // 直接取Linux/macOS命令输出的PID
                if (!line.trim().isEmpty()) {
                    pid = line.trim();
                    break;
                }
            }
        }
        process.waitFor();
        return pid;
    }

    public static void main(String[] args) {
        try {
            killProcessByPort(8080); // 替换为目标端口
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  • 权限问题:Windows下可能需要管理员权限终止系统进程;Linux下若进程属于其他用户,需在kill命令前加sudo(需额外处理密码输入逻辑)。
  • 命令兼容性:部分Linux发行版默认无lsof,代码中已做降级处理;不同系统的命令输出格式可能略有差异,需根据实际情况调整解析逻辑。
  • 异常处理:需处理端口未被占用、命令执行失败、权限不足等场景,避免程序崩溃。

内容的提问来源于stack exchange,提问作者Grimalkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 16:46:05