Getting information about processes

To get information about a process, we need access to the Info object of the process. This is available through ProcessHandle. We use a call to the handle.info() method to return it.

The Info interface defines query methods that deliver information about the process. These are:

  • command() returns Optional<String> containing the command that was used to start the process
  • arguments() returns Optional<String[]> that contains the arguments that were used on the command line after the command to start the process
  • commandLine() returns Optional<String> that contains the whole command line
  • startInstant() returns Optional<Instant>, which essentially represents the time the process was started
  • totalCpuDuration() returns Optional<Duration>, which represents the CPU time used by the process since it was started
  • user() returns Optional<String> that holds the name of the user the process belongs to

The values returned by these methods are all Optional because there is no guarantee that the operating system or the Java implementation can return the information. However, on most operating systems it should work and the returned values should be present.

The following sample code displays the information on a given process:

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;

public class ProcessHandleDemonstration {
public static void main(String[] args) throws InterruptedException,
IOException {
provideProcessInformation(ProcessHandle.current());
Process theProcess = new
ProcessBuilder("SnippingTool.exe").start();
provideProcessInformation(theProcess.toHandle());
theProcess.waitFor();
provideProcessInformation(theProcess.toHandle());
}
static void provideProcessInformation(ProcessHandle theHandle) {
// get id
long pid = ProcessHandle.current().pid();

// Get handle information (if available)
ProcessHandle.Info handleInformation = theHandle.info();

// Print header
System.out.println("|=============================|");
System.out.println("| INFORMATION ON YOUR PROCESS |");
System.out.println("|=============================| ");

// Print the PID
System.out.println("Process id (PID): " + pid);
System.out.println("Process Owner: " +
handleInformation.user().orElse(""));

// Print additional information if available
System.out.println("Command:" +
handleInformation.command().orElse(""));
String[] args = handleInformation.arguments().orElse (new String[]{});
System.out.println("Argument(s): ");
for (String arg: args) System.out.printf(" " + arg);
System.out.println("Command line: " +
handleInformation.commandLine().orElse(""));
System.out.println("Start time: " +
handleInformation.startInstant().orElse(Instant.now()).
toString());
System.out.printf("Run time duration: %sms%n",
handleInformation.totalCpuDuration().
orElse(Duration.ofMillis(0)).toMillis());
}
}

Here is the console output for the preceding code:

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset