I need to get the PID of a process which is launched via Java's Runtime.getRuntime().exec() command.
I know how to do it in JNA. But I really would like to do it with JNI and create my own libraries. Does anyone know how to do it?
import java.lang.reflect.Field;
class GetPid
{
public native int getPid( long procHandle);
static
{
System.loadLibrary("getpid");
}
public static void main(String args[])
{
try {
Process process = Runtime.getRuntime().exec( "calc");
Field f = process.getClass().getDeclaredField( "handle");
f.setAccessible( true);
long procHandle = f.getLong( process);
System.out.println( "prochandle: " + procHandle + ", pid: " + new GetPid().getPid( procHandle));
} catch( Exception e) {
e.printStackTrace();
}
}
}
But what's the C part supposed to look like?
JNIEXPORT jint JNICALL
Java_GetPid_getPid(JNIEnv *env, jobject obj, jlong handle)
{
...
return ???;
}
It would be great if someone could help me. I mainly seek the Windows solution, since you can get the PID for Linux from the Process field, but I wouldn't mind if someone could show me how to do this in Linux / Solaris as well.
Thank you very much in advance!