Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

How to find out my java process PID number on XP

Need to find out my Java program process number (PID) on Windows XP and write it out in a file. (to be used by another monitoring process).

thanks, JBB
0
jerzyb
Asked:
jerzyb
  • 4
  • 3
  • 3
  • +1
1 Solution
 
objectsCommented:
Java does not provide a platform independant way to get the id, you'll need to implement a platform specific method using exec() or JNI.
0
 
jerzybAuthor Commented:
Platform is XP and I am very dependent on it :-).
So how can I do it?

JBB
0
 
DeanHorakCommented:
From a batch file you can execute "tasklist" and pipe the output to a file. Then you can read through the file (using grep, awk or some other utility) and find your PID.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
run tasklist using exec() and parse the output
or call win32 api using jni: http://support.microsoft.com/kb/q175030/
0
 
jerzybAuthor Commented:
but what is my PID? thats what I want to know.

For example, in Perl, the variable $$ is the program's PID number.
How can I find it from inside a Java program?

JBB
0
 
objectsCommented:
> How can I find it from inside a Java program?

you need to call the OS, you cannot get it directly from java it is not available
0
 
jerzybAuthor Commented:
ok, maybe I should not have posted this under Java Programming topic then

my problem remains unsolved: I have a Java program (cant change that) and still need to know the PID of this Java program (and it has to be done from within, otherwise how do I distinguish between multiple instances of the same program under tasklist ?).

via exec(), call to OS or whatever, I am not too picky but need details

can anybody help ?
JBB
0
 
objectsCommented:
yes its primaryily an OS question
using exec() you'd call tasklist and parse the output
or using JNI you'd call the win32 api.
0
 
DeanHorakCommented:
The only way to get this and be sure you have the correct PID is to call the GetCurrentProcessId() Win32 function using JNI.

To do this, you basically have to do the following steps:

---------------------------------------
1.  Write the Java code for the application
---------------------------------------
class ShowPid {
    public static void main(String args[]) {
          System.out.println("My PID is "+GetPidValue());
    }

    static {
      System.loadLibrary("GetPid");  // <---- native DLL name
    }

  public static native int GetPidValue();  // <-- native function call
}

---------------------------------------
2. Run javah.exe on your .class file to generate a C header file
---------------------------------------
javah ShowPid

This will generate a file called ShowPid.h

3. Write the implementation of your native methods (in C/C++)
#include "jni.h"
#include "ShowPid.h"
#include <stdio.h>
#include<windows.h>

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    return TRUE;
}

JNIEXPORT int JNICALL Java_GetPid_GetPidValue(JNIEnv *env, jclass obj)
{
return GetCurrentProcessId() ;
}

------------------------------------
4. create the shared library
------------------------------------
Using you favorite C/C++ compiler to create a DLL (in this example called GetPid.dll). In Visual C++ just create a new Win32 project and then under "application settings" in the project creation wizard select DLL. Replace the generated code with something along the lines of the example above..

I didn't test this code, but it should be pretty close to what you need.

If you need more info a good description can be found at:
http://bdn.borland.com/article/0,1410,20679,00.html
0
 
DeanHorakCommented:
I had a chance to try out the code and found a couple of issues.

1) The C++ code should be changed to:

#include "ShowPid.h"
#include <stdio.h>
#include<windows.h>

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    return TRUE;
}

JNIEXPORT jint JNICALL Java_ShowPid_GetPidValue(JNIEnv *env, jclass obj)
{
return GetCurrentProcessId() ;
}

My earlier code had an include for "jni.h" which is actually included in the generated "ShowPid.h" file. I also had the name of the JNI function call incorrect.

2) You'll need to make sure you add the "<JDK-install-directory>\include" and "<JDK-install-directory>\include\win32" directories to your C++ INCLUDE path.

3) The dll will need to be in a directory listed the PATH environment variable. In most cases ".\" is in the PATH variable so the dll can reside in the same directory as the ShowPid.class file.

Other than that, I tested the code and it works.

There is a caveat you should be aware of, although it "probably" won't affect you.

The JVM can be run in one of two flavors; either client mode or server mode. The mode is set with the -client or -server switch on the java command line.

By default, 32-bit windows runs in client mode while 64-bit windows defaults to server mode. The difference is important because if the JVM is run in server mode, all java programs share the same JVM and will result in the same PID.
0
 
sciuriwareCommented:
I heard that System.getPid() would be part of JAVA 6.0 (Mustang).
You might download the Beta version from SUN for a tryout.
(first download the release notes and the JAVADOC).

;JOOP!
0
 
sciuriwareCommented:
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now