Detecting the platform (Window or Linux) by Groovy/Grails

瘦欲@ 提交于 2019-12-05 08:40:27

问题


Is there a way to detect the platform (Window / Linux) in which the website is running by Groovy / Grails?


回答1:


System.properties['os.name']

will return the name of the OS, e.g. "Windows XP". So if you want to figure out whether you're running on Windows or not, you could do something like:

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

Alternatively, org.apache.commons.lang.SystemUtils (from the Apache commons-lang project) exposes some boolean constants that provide the same information as the code above, e.g.

SystemUtils.IS_OS_MAC
SystemUtils.IS_OS_WINDOWS
SystemUtils.IS_OS_UNIX

More specific constants such as these are also available

SystemUtils.IS_OS_WINDOWS_2000
SystemUtils.IS_OS_SOLARIS
SystemUtils.IS_OS_MAC_OSX



回答2:


Or for short:

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

Since Groovy provides a map access to getAt/putAt methods.



来源:https://stackoverflow.com/questions/4689240/detecting-the-platform-window-or-linux-by-groovy-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!