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

Virtual Threads in Java 21: Simplified Concurrency for Modern Applications

  With Java 21, Virtual Threads have redefined how we approach concurrency, offering a lightweight and efficient way to handle parallel and ...