Saturday, August 1, 2020

How to detect OS in kotlin

In kotlin also you can use System.getProperty("os.name") for detect type OS.

Check example class test.kt

fun main(args: Array<String>) {

val OS = System.getProperty("os.name").toLowerCase()

when {
OS.indexOf("win") >= 0 -> {
print("Windows")
}
OS.indexOf("mac") >= 0 -> {
print("Mac")
}
(OS.indexOf("nix") >= 0
|| OS.indexOf("nux") >= 0
|| OS.indexOf("aix") > 0) -> {
print("Unix or Linux")
}
OS.indexOf("sunos") >= 0 -> {
print("Solaris")
}
else -> print("Others")
}


}






References:
http://lopica.sourceforge.net/os.html
https://mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname

No comments:

Post a Comment

Integrating Google Cloud Pub/Sub with Terraform and Spring Boot 3 (Java 21)

Introduction In this blog post, I'll demonstrate how to provision Google Cloud Pub/Sub resources using Terraform and integrate them with...