ProcessHandle Inspects Current Process
Read the JVM itself through ProcessHandle.current(): pid, command, start time, and child processes, with every detail wrapped in Optional.
Code
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class SelfCheck {
public static void main(String[] args) {
ProcessHandle self = ProcessHandle.current();
System.out.println("pid = " + self.pid());
ProcessHandle.Info info = self.info();
// details like the command line and start time come from the runtime, always wrapped in Optional
System.out.println("command = " + info.command().orElse("(unavailable)"));
System.out.println("start = " + info.startInstant()
.map(instant -> instant.atZone(ZoneId.systemDefault())
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
.orElse("(unavailable)"));
// descendant enumeration: the entry point for liveness checks or process-tree walks
System.out.println("children = " + self.children().count());
}
}How it works
ProcessHandle Inspects Current Process reduces to the class SelfCheck and the function main.
Using it
Running the file prints labeled results such as pid =, command =, start = — each operation’s effect is visible in the output.
Key statements
.map(instant -> instant.atZone(ZoneId.systemDefault())
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does ProcessHandle Inspects Current Process print when run?
- No setup needed: run the file and look for labeled lines like `pid =`, `command =`, `start =` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.