Search code examples
javamacosoperating-systemarm64

Detect MacOS arm64 and x86/x64 in Java


I dont have access to a mac and I need to detect if the MacOS currently running is ARM64 or x64/x86 in java programmatically. Its going to be used to download chromedriver from chrome-for-testing.

Currently, I'm using this code to download:

String osName = System.getProperty("os.name", "").toLowerCase();
if(osName.contains("darwin") || osName.contains("mac")) {
    // FIXME: no check for ARM64 arch
    _zipName = chromeForTesting ? "chromedriver-mac-x64.zip" : "chromedriver_mac64.zip";
}

Solution

  • something along the following (edited and tested on macpro)

    cat GetSystemProperty.java 
    public class GetSystemProperty {
    
        public static void main(String[] args) {
            String osName = System.getProperty("os.name");
            String osArch = System.getProperty("os.arch");
    
            System.out.println("OS name: " + osName);
            System.out.println("OS architecture: " + osArch);
        }
    }
    
    java GetSystemProperty
    OS name: Mac OS X
    OS architecture: x86_64
    
    MacBook-Pro-15: glennstevenson$ 
    uname -a
    MacBook-Pro-15: 20.6.0 Darwin Kernel Version 20.6.0: Thu Jul  6 22:12:47 PDT 2023; root:xnu-7195.141.49.702.12~1/RELEASE_X86_64 x86_64
    
    

    NB: a case-insensitive test may be wisest!

    As always, test before use !