如何以编程方式确定 Java 中的操作系统? [英] How do I programmatically determine operating system in Java?

查看:29
本文介绍了如何以编程方式确定 Java 中的操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式确定我的 Java 程序正在运行的主机的操作系统(例如:我希望能够根据我是在 Windows 还是 Unix 平台上加载不同的属性).以 100% 的可靠性做到这一点的最安全方法是什么?

I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different properties based on whether I am on a Windows or Unix platform). What is the safest way to do this with 100% reliability?

推荐答案

您可以使用:

System.getProperty("os.name")

附言您可能会发现此代码很有用:

P.S. You may find this code useful:

class ShowProperties {
    public static void main(String[] args) {
        System.getProperties().list(System.out);
    }
}

它所做的只是打印出您的 Java 实现提供的所有属性.它会让您了解您可以通过属性了解您的 Java 环境.:-)

All it does is print out all the properties provided by your Java implementations. It'll give you an idea of what you can find out about your Java environment via properties. :-)

这篇关于如何以编程方式确定 Java 中的操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆