Created
December 6, 2013 15:36
-
-
Save deckerego/7826661 to your computer and use it in GitHub Desktop.
Finding The Current Process ID
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
import java.io.IOException; | |
import java.util.UUID; | |
import com.sun.tools.attach.AttachNotSupportedException; | |
import com.sun.tools.attach.VirtualMachine; | |
import com.sun.tools.attach.VirtualMachineDescriptor; | |
public class MyPID { | |
public static void main(String[] args) throws AttachNotSupportedException, IOException { | |
String uniqueId = UUID.randomUUID().toString(); | |
System.setProperty("process.unique.id", uniqueId); | |
for(VirtualMachineDescriptor descriptor : VirtualMachine.list()) { | |
String remoteUniqueId = VirtualMachine.attach(descriptor).getSystemProperties().getProperty("process.unique.id"); | |
if(remoteUniqueId != null && remoteUniqueId.equals(uniqueId)) | |
System.out.println("This PID: "+descriptor.id()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment