如何在 Java 中检测类 Unix 操作系统? [英] How can I detect a Unix-like OS in Java?
问题描述
好的,我知道 System.getProperty("os.name")
会给我我正在运行的操作系统的名称,但这并没有多大帮助.我需要知道的是,我运行的操作系统是否是类 Unix"操作系统,我不在乎它是 HP-UX、AIX、Mac OS X 还是其他什么.
Ok, I know that System.getProperty("os.name")
will give me the name of the OS I'm running under, but that's not a lot of help. What I need to know is if the OS I'm running on is a 'Unix-like' OS, I don't care if it's HP-UX, AIX, Mac OS X or whatever.
从 可能的 os.name 值列表,这似乎是一种快速而肮脏的方式检测类 Unix"操作系统是检查 os.name
是否 not 包含Windows".会给我带来的误报是我的代码不太可能遇到的操作系统!不过,如果有更好的方法,我还是很想知道.
From the list of possible os.name values it seems like a quick and dirty way of detecting a 'Unix-like' OS is checking if os.name
does not contain "Windows". The false positives that will give me are OSes my code is very unlikely to encounter! Still, I'd love to know a better way if there is one.
推荐答案
我已经在 Windows XP、Vista、Win7、Mac OS 10.3 - 10.6 和各种 Linux 发行版上的生产代码中使用了您的方案,没有出现问题:
I've used your scheme in production code on Windows XP, Vista, Win7, Mac OS 10.3 - 10.6 and a variety of Linux distros without an issue:
if (System.getProperty("os.name").startsWith("Windows")) {
// includes: Windows 2000, Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP
} else {
// everything else
}
本质上,通过不检测Windows来检测类Unix.
Essentially, detect Unix-like by not detecting Windows.
这篇关于如何在 Java 中检测类 Unix 操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!