Java 9 provied cách khá để có được thông tin của Process
, nhưng tôi vẫn không biết làm thế nào để có được những CommandLine
& arguments
của quá trình:Làm thế nào để có được commandline & đối số của quá trình trong Java 9
Process p = Runtime.getRuntime().exec("notepad.exe E:\\test.txt");
ProcessHandle.Info info = p.toHandle().info();
String[] arguments = info.arguments().orElse(new String[]{});
System.out.println("Arguments : " + arguments.length);
System.out.println("Command : " + info.command().orElse(""));
System.out.println("CommandLine : " + info.commandLine().orElse(""));
Kết quả:
Arguments : 0
Command : C:\Windows\System32\notepad.exe
CommandLine :
Nhưng tôi đang mong đợi:
Arguments : 1
Command : C:\Windows\System32\notepad.exe
CommandLine : C:\Windows\System32\notepad.exe E:\\test.txt
Chỉ cần gỡ lỗi thêm, bạn có thể thử thay thế ProcessHandler 'p.toHandle()' bằng 'ProcessHandle.current()' và thực hiện để xem, nếu bạn nhận được một số giá trị trong trường dự kiến? Chủ yếu là quan tâm để xem nếu xử lý quá trình của quá trình hiện tại của bạn là thích hợp hay không. – nullpointer
Không may mắn, vẫn như cũ. – Jerry06