Last active
December 23, 2021 13:26
-
-
Save barend/278d1edd93b1ab23fc16064cb2aa2a07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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