Paw*_*wka 14 java linux process
我正在以这种方式创建子进程:
String command = new String("some_program");
Process p = Runtime.getRuntime().exec(command);
Run Code Online (Sandbox Code Playgroud)
我如何获得该子进程ID?
PS我在Linux上工作.
Pas*_*ent 28
仍然没有公共API(请参阅http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4244896),但有一些解决方法.
第一个解决方法是使用外部程序,ps
并使用它来调用它Runtime.exec()
来获取pid :)
另一个是基于这样一个事实,即java.lang.Process
类是抽象的,并且您实际上根据您的平台获得了一个具体的子类.在Linux上,你会得到java.lang.UnixProcess
一个私有字段int pid
.使用反射,您可以轻松获得此字段的值:
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
System.out.println( f.get( p ) );
Run Code Online (Sandbox Code Playgroud)