没有 process.pid(); // get the pid
时间: 2023-11-27 12:03:06 浏览: 26
非常抱歉,我犯了一个错误。在 Java 中,获取进程 ID 的正确方法是使用 `ProcessHandle` 类。以下是一个使用 `ProcessHandle` 获取进程 ID 的示例:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LinuxCommandExecutor {
public static void main(String[] args) {
String command = "your linux command here";
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
reader.lines().forEach(line -> {
// do something with the output
});
long pid = process.toHandle().pid(); // get the pid
process.waitFor();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,`Process` 类现在提供了一个名为 `toHandle()` 的方法,该方法返回一个 `ProcessHandle` 对象,可以用来获取进程 ID。