2011-01-14 34 views

Trả lời

50
System.properties['os.name'] 

sẽ trả về tên của hệ điều hành, ví dụ: "Windows XP". Vì vậy, nếu bạn muốn tìm hiểu xem bạn đang chạy trên Windows hay không, bạn có thể làm một cái gì đó như:

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

Ngoài ra, org.apache.commons.lang.SystemUtils (từ dự án Apache commons-lang) cho thấy một số hằng boolean cung cấp các thông tin tương tự như mã ở trên, ví dụ

SystemUtils.IS_OS_MAC 
SystemUtils.IS_OS_WINDOWS 
SystemUtils.IS_OS_UNIX 

hằng cụ thể hơn như thế này cũng có sẵn

SystemUtils.IS_OS_WINDOWS_2000 
SystemUtils.IS_OS_SOLARIS 
SystemUtils.IS_OS_MAC_OSX 
Các vấn đề liên quan