How to determine the PID of your Java program

Discussion in 'Java' started by andreww100@gmail.com, Jul 24, 2005.

  1. Guest

    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);
    }
    }
    }
    }
     
    , Jul 24, 2005
    #1
    1. Advertising

  2. On 24 Jul 2005 08:56:43 -0700, wrote:

    > 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?

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Presented in BC [Brain Control] Where Available
     
    Andrew Thompson, Jul 24, 2005
    #2
    1. Advertising

  3. Guest

    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

    Andrew Thompson wrote:
    > On 24 Jul 2005 08:56:43 -0700, andreww wrote:
    >
    > > 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?
    >
    > --
    > Andrew Thompson
    > physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    > Presented in BC [Brain Control] Where Available
     
    , Jul 24, 2005
    #3
  4. On Sun, 24 Jul 2005 14:09:38 -0700, andreww100 wrote:

    > 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.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 24, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    5
    Views:
    498
    Ben Finney
    Dec 28, 2003
  2. David Hirschfield
    Replies:
    3
    Views:
    3,322
    Thomas Guettler
    Feb 3, 2006
  3. Lars =?iso-8859-15?Q?Gust=E4bel?=

    Re: Best way to determine if a certain PID is still running

    Lars =?iso-8859-15?Q?Gust=E4bel?=, Feb 3, 2006, in forum: Python
    Replies:
    2
    Views:
    461
    MrJean1
    Feb 4, 2006
  4. Joey

    Determine a child PID

    Joey, Jul 15, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    129
    Jeff 'japhy' Pinyan
    Jul 15, 2003
  5. Hal Vaughan
    Replies:
    7
    Views:
    136
    Joe Smith
    Sep 6, 2005
Loading...

Share This Page