【问题标题】:Detecting the platform (Window or Linux) by Groovy/Grails通过 Groovy/Grails 检测平台(Window 或 Linux)
【发布时间】:2011-06-09 00:32:12
【问题描述】:

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

【问题讨论】:

    标签: grails groovy platform-detection


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      或简称:

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

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

      【讨论】:

        猜你喜欢
        • 2013-06-05
        • 2011-04-06
        • 2014-11-16
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多