java查询linux的操作系统版本方法
时间: 2023-09-13 22:12:41 浏览: 88
在Java中查询Linux的操作系统版本可以通过以下代码实现:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OSInfo {
private static String OS = System.getProperty("os.name").toLowerCase();
public static void main(String[] args) {
System.out.println(getOSVersion());
}
public static String getOSVersion() {
String version = "";
if (isLinux()) {
ProcessBuilder pb = new ProcessBuilder("cat", "/etc/os-release");
try {
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("VERSION_ID=")) {
version = line.substring(line.indexOf("=") + 1).replaceAll("\"", "");
break;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return version;
}
public static boolean isLinux() {
return (OS.indexOf("linux") >= 0);
}
}
```
该代码会在Linux系统中执行cat /etc/os-release命令,并通过读取命令输出的内容来获取Linux的操作系统版本信息。
阅读全文