Skip to content

Instantly share code, notes, and snippets.

@deckerego
Created December 6, 2013 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckerego/7826661 to your computer and use it in GitHub Desktop.
Save deckerego/7826661 to your computer and use it in GitHub Desktop.
Finding The Current Process ID
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