title: Get PID of Runtime process using JNI
body: <p>I need to get the PID of a process which is launched via Java's Runtime.getRuntime().exec() command.</p>
  
  <p>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?</p>
  
  <pre><code>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();
          }
  
      }
  }
  </code></pre>
  
  <p>But what's the C part supposed to look like?</p>
  
  <pre><code>JNIEXPORT jint JNICALL
  Java_GetPid_getPid(JNIEnv *env, jobject obj, jlong handle)
  {
      ...
  
      return ???;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thank you very much in advance!</p>