java 执行linux命令并获取pid
时间: 2023-08-04 07:15:46 浏览: 99
java执行linux命令
5星 · 资源好评率100%
你可以使用Java中的Runtime类来执行Linux命令。以下是一个简单的示例,可以执行Linux命令并获取其pid:
```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()));
String line;
while ((line = reader.readLine()) != null) {
// do something with the output
}
int pid = process.pid(); // get the pid
process.waitFor();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,此代码仅是示例,你需要将“your linux command here”替换为你要执行的实际命令。
阅读全文