How to determine the PID of your Java program

A

andreww100

Hi

I was unable to find a decent How-To for finding the PID of a Java
program running on Windows, that did not use JNI. Here is a solution
that uses netstat.

Hope this is useful. If you have a better strategy, let me know!

Thanks

Andrew


package pid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;

/**
* Program to obtain the Process Identifier (PID) of the running Java
program
* Assumes the Windows program
* @author Andrew Ward
*/
public class GetPID {

public static void main(String[] args) {
getPid();
}

/**
* Obtain the PID, by listening on a port, then using netstat -ano
* to find the PID of the processing listening on the port
* @return the pid of this process
*/
public static int getPid() {

// Select a port between 50000 and 51000
final int port = 50000 + (int) (Math.round(Math.random() * 1000));

// Windows specific command line
// Netstat -ano will return many lines, one of which will match
// TCP 0.0.0.0:<port> 0.0.0.0:0 LISTENING
<pid>
final String cmd = "netstat -ano";
final String criteria = "0.0.0.0:" + port;

// Listen on the port for 5 seconds
new SocketListener(port, 5 * 1000).start();

int pid = -1;

try {
// Run netstat
Process process = Runtime.getRuntime().exec(cmd);

InputStream istr = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));

String str;
while ((str = br.readLine()) != null) {
if (str.indexOf(criteria) > 0) {
String match = str.substring(1+str.lastIndexOf(" "));
pid = Integer.valueOf(match);
System.out.println("PID: " + pid + " {" + str + "}");
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}

return pid;
}

/** listen on a socket */
public static class SocketListener extends Thread {
int port;

int wait;

public SocketListener(int port, int wait) {
this.port = port;
this.wait = wait;
}

public void run() {
try {
ServerSocket server = new ServerSocket(this.port);
server.setSoTimeout(this.wait);
server.accept();
server.close();
}
catch(SocketTimeoutException e)
{
// We are expecting the accept() call to timeout
// so ignore this exception
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}
}
 
A

Andrew Thompson

I was unable to find a decent How-To for finding the PID of a Java
program running on Windows, that did not use JNI.

If it is a Win specific question, what is the problem
with using JNI?
 
A

andreww100

Hi Andrew

I may want a Linux version next, so I would simply work out the
equivalent Linux command. This class is to be used in an example
program, so I wanted to avoid having to bundle native binaries.

Andrew
 
S

Stefan Schulz

I may want a Linux version next, so I would simply work out the
equivalent Linux command. This class is to be used in an example
program, so I wanted to avoid having to bundle native binaries.

Maybe i misunderstand what i try to do, but black magic of this kind
almost never works as intended for a demonstration. You don't explain how
a linear mapping works by using the vector space of polynomials, either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top