Re: Finding java process PID number on Windows XPreply 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 XPreply 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 XPreply 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 XPreply 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 XPreply 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 XPreply 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!
|