在Java 9 Process API中,负责控制和管理操作系统进程的API已得到很大改进。 ProcessHandle类提供了检查进程活动性和销毁进程的方法。它具有onExit方法,CompletableFuture类可以在进程退出时异步执行操作。
import java.time.ZoneId; import java.util.stream.Stream; import java.util.stream.Collectors; import java.io.IOException; public class Tester { public static void main(String[] args) throws IOException { ProcessBuilder pb=new ProcessBuilder("notepad.exe"); String np="Not Present"; Process p=pb.start(); ProcessHandle.Info info=p.info(); System.out.printf("Process ID : %s%n", p.pid()); System.out.printf("Command name : %s%n", info.command().orElse(np)); System.out.printf("Command line : %s%n", info.commandLine().orElse(np)); System.out.printf("Start time: %s%n", info.startInstant().map(i -> i.atZone(ZoneId.systemDefault()) .toLocalDateTime().toString()).orElse(np)); System.out.printf("Arguments : %s%n", info.arguments().map(a -> Stream.of(a).collect( Collectors.joining(" "))).orElse(np)); System.out.printf("User : %s%n", info.user().orElse(np)); } }
您将看到以下输出。
Process ID : 5800 Command name : C:\Windows\System32\notepad.exe Command line : Not Present Start time: 2017-11-04T21:35:03.626 Arguments : Not Present User: administrator
参考链接
https://www.learnfk.com/java/java9-process-api-improvements.html
标签:info,Process,无涯,System,API,printf,np,s%,out From: https://blog.51cto.com/u_14033984/8987457