java - 如何确定 Java 是否在 Windows 上运行
问题描述
我希望确定 Java 是否在 Windows 上运行,并且看到了许多不同的建议,其中包括 System 属性的各种排列os.name
/ startsWith
/ indexOf
/ contains
/ toLowerCase(Locale.ENGLISH)
,toLowerCase()
或者只是File.separatorChar
.
我扫描了 JDK 源代码以查看是否有明确的答案(见下文)以及其他一些建议的 SO 帖子:
String os = System.getProperty("os.name" /**, "<Surely os.name is never null?>" */);
List<Boolean> isWindows = List.of(
os.startsWith("Windows"),
os.contains("Windows"),
os.toLowerCase().startsWith("windows"),
os.toLowerCase().contains("windows"),
os.toLowerCase(Locale.ENGLISH).contains("windows"),
os.toLowerCase(Locale.ENGLISH).startsWith("windows"),
File.separatorChar == '\\'
);
System.out.println("os.name ="+os);
System.out.println("os.name(UTF-8)="+Arrays.toString(os.getBytes(StandardCharsets.UTF_8)));
System.out.println("isWindows ="+isWindows);
isWindows
是否存在使用上述条件错误识别的操作系统/语言安装排列,其中真/假不一致或错误?
// For Windows I would expect all true, such as:
os.name =Windows 10
os.name(UTF-8)=[87, 105, 110, 100, 111, 119, 115, 32, 49, 48]
isWindows =[true, true, true, true, true, true, true]
// For non-Windows I would expect all false such as:
os.name =Linux
os.name(UTF-8)=[76, 105, 110, 117, 120]
isWindows =[false, false, false, false, false, false, false]
JDK 源代码示例
作为参考,这是isWindows
在 Open JDK17 源代码中检测到的位置(来自最近的git fetch
非最终候选版本):
src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java
System.getProperty("os.name").toLowerCase().startsWith("win"));
src/java.smartcardio/share/classes/sun/security/smartcardio/CardImpl.java
src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/JNIWriter.java
isWindows = System.getProperty("os.name").startsWith("Windows"))
src/java.desktop/share/classes/sun/font/FontUtilities.java
isWindows = System.getProperty("os.name", "unknownOS").startsWith("Windows");
src/java.base/share/classes/java/util/zip/ZipFile.java
src/java.desktop/share/classes/sun/awt/OSInfo.java
VM.getSavedProperty("os.name").contains("Windows")
System.getProperty("os.name").contains("Windows")
src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusLookAndFeel.java
boolean isWindows = osName != null && osName.contains("Windows");
src/java.desktop/share/classes/javax/swing/plaf/basic/BasicFileChooserUI.java
boolean isWin32 = (File.separatorChar == '\\')
src/jdk.jpackage/share/classes/jdk/jpackage/internal/Platform.java
Note that I've omitted this test in case of confusion with Darwin OS:
String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("win") >= 0) {
platform = Platform.WINDOWS;
}
解决方案
在注意到各种评论和其他帖子之后,我没有找到不继续使用isWindows
我目前在代码中进行的检查的理由,这只是列出的第一个测试:
boolean isWindows = System.getProperty("os.name").startsWith("Windows");
我的问题中的前六个测试似乎可以isWindows
正确检测,但不是最后一项File.separatorChar == '\\'
(在 OS/2 上也是如此)。
请注意,在土耳其语中,“I”的小写值不等于“i”。尝试:
"I".toLowerCase(Locale.forLanguageTag("TR")).equals("i") // char:305
"i".toUpperCase(Locale.forLanguageTag("TR")).equals("I") // char:304
也许这解释了为什么一些开发人员使用.toLowerCase(Locale.ENGLISH)
但没有任何变化的任何测试结果,除非os.name
属性在土耳其安装中是大写的。
感谢所有的建议。