Java, windows: Get process name of given PID

2019-07-20 04:13发布

问题:

Running on windows.

I need to know from within my program which processes (like Skype) are running on port :80.

I should be able to get the PID of the process running on port :80 via

netstat -o -n -a | findstr 0.0:80.

What would be the best way to get the name of a process with a given PID from within Java?

If there is any way to get the name of the process running on port :80 from within Java I would prefere that also.

The reason I need this is that my application launches a jetty web server which uses port :80. I want to warn the user that an other service is already running port :80 in case.

回答1:

So I got it working using two Process. One for gettinng the PID and one for getting the name.

Here it is:

private String getPIDRunningOnPort80() {
        String cmd = "cmd /c netstat -o -n -a | findstr 0.0:80";

        Runtime rt = Runtime.getRuntime();
        Process p = null;
        try {
            p = rt.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringBuffer sbInput = new StringBuffer();
        BufferedReader brInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader brError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        String line;
        try {
            while ((line = brError.readLine()) != null) {
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            while ((line = brInput.readLine()) != null) {
                sbInput.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            p.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        p.destroy();

        String resultString = sbInput.toString();
        resultString = resultString.substring(resultString.lastIndexOf(" ", resultString.length())).replace("\n", "");
        return resultString;
    }



private String getProcessNameFromPID(String pid) {
    Process p = null;
    try {
        p = Runtime.getRuntime().exec("tasklist");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    StringBuffer sbInput = new StringBuffer();
    BufferedReader brInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    String foundLine = "UNKNOWN";
    try {
        while ((line = brInput.readLine()) != null) {
            if (line.contains(pid)){
                foundLine = line; 
            }
            System.out.println(line);
            sbInput.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    p.destroy();

    String result = foundLine.substring(0, foundLine.indexOf(" "));



    return result;
}