Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a leaner way to do this using Java JNI. </p> <p>This works definitely for Windows and Linux, i assume that you can do the same for other platforms too. </p> <p>The biggest problem of Java process handling is the lack of a method to get the process id of the process started with untime.getRuntime().exec(). </p> <p>Assuming you got the pid of a process, you always can start a kill -9 command in linux, or use similar ways to kill a process in windows. </p> <p>Here is a way to get the process id natively for linux (borrowed from the selenium framework, :) ), and with the help of JNI this also can be done for windows (using native Windows API calls). </p> <p>For this to work (for Windows) you first have to get the JNI Library at <a href="http://java.net/projects/jna/downloads/" rel="nofollow">JAVA NATIVE ACCESS (JNA): Downloads</a></p> <p>Look at the following code, which will get the pid of a (in this example windows) program (most of the code is actually debris to get a working java program going):</p> <pre><code>import com.sun.jna.*; import java.lang.reflect.Field; import java.util.logging.Level; import java.util.logging.Logger; public class Main { static interface Kernel32 extends Library { public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); public int GetProcessId(Long hProcess); } public static void main(String[] args) { try { Process p; if (Platform.isWindows()) p = Runtime.getRuntime().exec("cmd /C ping msn.de"); else if (Platform.isLinux()) p = Runtime.getRuntime().exec("cmd /C ping msn.de"); System.out.println("The PID: " + getPid(p)); int x = p.waitFor(); System.out.println("Exit with exitcode: " + x); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } public static int getPid(Process p) { Field f; if (Platform.isWindows()) { try { f = p.getClass().getDeclaredField("handle"); f.setAccessible(true); int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p)); return pid; } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } else if (Platform.isLinux()) { try { f = p.getClass().getDeclaredField("pid"); f.setAccessible(true); int pid = (Integer) f.get(p); return pid; } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } else{} return 0; } } </code></pre> <p>Hope this helps, ;)...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload