Does kotlin offer a way to check if the jvm is run...
# getting-started
m
Does kotlin offer a way to check if the jvm is running on arm64 or x64? I can only find references to java os.arch but the only documented value seems to be
amd64
for x64 (or maybe both? not very clear)
c
You can use
os.arch
for that, though with some caveats. Using this, for example:
Copy code
public open val arch: Arch by lazy {
        when (val archName = System.getProperty("os.arch", "unknown").lowercase()) {
            "amd64", "x86_64" -> Arch.Amd64
            "arm64", "aarch64" -> Arch.Arm64
            else -> error("Unknown architecture $archName")
        }
    }
However, be aware that this only reports the JVM’s view on the architecture; for example, an x86_64 JVM on an Apple Arm M1 (using Rosetta) will report x86_64 (as its an x86_64 JVM), even though the OS is aarch64.
If you want to know the underlying actual architecture for aa JVM app on OS X:
Copy code
public object MacOsX : Os() {
        public val appleSilicon: Boolean by lazy { execAndGetStdout("uname", "-p") == "arm" }
        public val translatedByRosetta: Boolean by lazy {
            execAndGetStdout(
                "sysctl",
                "sysctl.proc_translated"
            ) == "sysctl.proc_translated: 1"
        }
        public override val arch: Arch by lazy {
            when {
                appleSilicon -> Arch.Arm64
                else -> Arch.Amd64
            }
        }
    }