THE ZEPINT NETWORK

programmer assist

Java Java XML Feeds

Java Questions Java Solutions Java Articles

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

Finding java process PID number on Windows XP

DiggBlinkRedditDeliciousTechnorati

question by Bejaan | Moderate

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

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Tejas

Run tasklist using exec() and parse the output .. or .. call win32 api using jni: http://support.microsoft.com/kb/q175030/

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Bejaan

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?

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Tejas

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

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Bejaan

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 ?

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Tejas

Yes its primarily an OS question
using exec() you'd call tasklist and parse the output
or using JNI you'd call the win32 api.

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Srirangan

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

Post reply Subscriptions

Re: Finding java process PID number on Windows XP

reply by Srirangan

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.

Post reply Subscriptions

Got a Java Question?

Just Sign Up and ask the top Java experts!

Search via Google

User Login

Email Address

Password

Java Experts

Rank Expert Points
#1 Srirangan 100
This a list of the Top Java experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?