我想在 Java 程序运行时以编程方式获取操作系统名称。我试过了,System.getProperty("os.name")
但结果是"Linux"
。如何做到这一点?
答案1
我发现只有三个系统属性可以确定操作系统详细信息:
os.name : Linux
os.version : 3.13.0-74-generic
os.arch " amd64
ubuntu 特定的解决方案是读取这些文件:
/etc/*-release
为我 :
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS"
NAME="Ubuntu"
VERSION="14.04, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
和 Java 代码:
File file = new File("/etc");
File[] list = file.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("-release");
}
});
for (File f : list) {
Scanner s = new Scanner(new FileInputStream(f));
while (s.hasNext()) {
System.out.println(s.nextLine());
}
}