Skip to content

Instantly share code, notes, and snippets.

@barend
Last active December 23, 2021 13:26
Show Gist options
  • Save barend/278d1edd93b1ab23fc16064cb2aa2a07 to your computer and use it in GitHub Desktop.
Save barend/278d1edd93b1ab23fc16064cb2aa2a07 to your computer and use it in GitHub Desktop.
package io.github.barend.getpid;
import com.sun.jna.Library;
import com.sun.jna.Native;
/**
* requires {@code jna.jar}.
* http://stackoverflow.com/a/7303433/49489
*/
public class GetPid {
public long getPid() throws UnsatisfiedLinkError {
CLibrary libc;
try {
libc = (CLibrary) Native.loadLibrary("c", CLibrary.class);
} catch (UnsatisfiedLinkError e) {
libc = (CLibrary) Native.loadLibrary("System", CLibrary.class);
}
return libc.getpid();
}
private interface CLibrary extends Library {
long getpid();
}
}
public class GetPid {
public long getPid() {
return ProcessHandle.current().pid()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment