32

有没有办法通过 Groovy / Grails 检测网站运行的平台(Window / Linux)?

4

2 回答 2

63
System.properties['os.name']

将返回操作系统的名称,例如“Windows XP”。因此,如果您想确定您是否在 Windows 上运行,您可以执行以下操作:

if (System.properties['os.name'].toLowerCase().contains('windows')) {
    println "it's Windows"
} else {
    println "it's not Windows"
}

或者,org.apache.commons.lang.SystemUtils(来自Apache commons-lang项目)公开了一些布尔常量,它们提供与上述代码相同的信息,例如

SystemUtils.IS_OS_MAC
SystemUtils.IS_OS_WINDOWS
SystemUtils.IS_OS_UNIX

也可以使用更具体的常量,例如这些

SystemUtils.IS_OS_WINDOWS_2000
SystemUtils.IS_OS_SOLARIS
SystemUtils.IS_OS_MAC_OSX
于 2011-01-14T08:47:46.687 回答
3

或简称:

if (System.env['OS'].contains('Windows')){ println "it's Windows" }

getAt/putAt由于 Groovy 提供了对方法的映射访问。

于 2018-05-12T14:43:00.290 回答