有没有办法用java找到os名字?

spr*_*ran 8 java

有没有办法用java找到os名字?

我试过下面的代码,但它会返回看起来像(Linux,windows ..)

System.getProperty("os.name")
Run Code Online (Sandbox Code Playgroud)

我需要检测以下格式

Linux - "ubuntu,mandriva ..",windows - "xp,vista ......"

对不起我的英语不好 :-( !!!

任何的想法 ?

Man*_*qui 11

您可以使用System.getProperty()以获取以下属性:

  • os.name:操作系统名称
  • os.arch:操作系统架构
  • os.version:操作系统版本

在您的情况下,我相信您正在寻找该os.version物业.该Javadoc中System.getProperties()包含您可以检索属性的完整列表.

编辑

我刚刚在Linux Mint中对此进行了测试,看起来获取os.version属性实际上是返回内核版本,而不是发行版本:

Linux
amd64
2.6.38-8-generic
Run Code Online (Sandbox Code Playgroud)

在找到这篇文章之后,似乎没有可靠的方法来查找您通过Java API运行的Linux发行版.

如果您知道自己在Linux中运行,则可以从Java运行以下系统命令之一,但您必须grep /解析分发:

  • cat /etc/*-release

  • 实际上,`uname -a`不包含分发名称.(或者至少在Fedora上,它没有.)你应该使用"/ etc/system-release"...因为(至少在Fedora上)"/ etc/* - release"模式匹配3路径名. (2认同)

bri*_*ium 5

这可能对您有所帮助:

System.out.println("\nName of the OS: " + System.getProperty("os.name"));
System.out.println("Version of the OS: " + System.getProperty("os.version"));
System.out.println("Architecture of the OS: " + System.getProperty("os.arch"));
Run Code Online (Sandbox Code Playgroud)

编辑:这是它在Windows下返回的内容:

Name of the OS: Windows XP
Version of the OS: 5.1
Architecture of the OS: x86
Run Code Online (Sandbox Code Playgroud)