How can I determine which Operating System I use p...
# multiplatform
f
How can I determine which Operating System I use programmatically? I want to create a database file that will be stored in different places depending if the machine runs Windows or Linux.
j
you want to determine platform? Like
Android
,
iOS
,
Web
??
f
I want to determine OS for Desktop. Like
Linux
or
Windows
j
You can run javascript to get
OS
details. I am not web developer guy might you could find something on
stackoverflow
. https://stackoverflow.com/questions/9514179/how-to-find-the-operating-system-details-using-javascript
f
I am writing for Desktop (jvmMain), so I don't have access to JavaScript. Only Kotlin and Java
h
What do you get from
System.getProperty("os.name")
?
k
I want to create a database file that will be stored in different places depending if the machine runs Windows or Linux.
sounds like expect/actual case?
h
Should hardly think so, Krzysztof, since in both cases this will run on the JVM.
k
Yeah, but you can have per-platform case even there, right? like with targets:
Copy code
common:
expect fun getDbPath(): String

macos:
expect fun getDbPath(): String

mingw: 
expect fun getDbPath(): String

linux:
expect fun getDbPath(): String
h
Or just stuff them all into the JVM platform and do a simple if/then when picking the path?
fun getDbPath(os: String) = if os.contains("linux") "linux-path" else "windows-path"
k
of course, it's just that it's more error prone to do it that way
c
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/
SystemUtils
has several utilities for this: •
IS_OS_WINDOWS
IS_OS_LINUX
• …etc
h
Copy code
private val isWindows = (FileSystems.getDefault().separator == "\\")
🪟 1