Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect apple silicon (M1) vs. intel in java?

Tags:

java

(For everyone who does not understand the question, please note, the os.arch property will only give you the architecture of the JRE, not of the underlying OS, which does not answer my question)

If you install a 32 bit jre on a 64 bit system, System.getProperty("os.arch") will return x86

In order to actually determine the underlying architecture, you will need to write some native code.

I want my app to do something if the processor is intel and something else if my processor is apple silicon.

I tried with

System.getProperties().list(System.out);
System.out.println(System.getProperty("os.arch"));

but on intel os.arch is the same value as in apple silicon = x86_64

like image 335
Andrei27 Avatar asked Oct 20 '25 01:10

Andrei27


1 Answers

You have to get this information from the operating system. On Windows, there is an environment variable – PROCESSOR_IDENTIFIER – which you can obtain via method getenv, as in:

System.getenv("PROCESSOR_IDENTIFIER");

On my Windows 10 machine, I get:

Intel64 Family 6 Model 158 Stepping 11, GenuineIntel

I don't have Mac but according to this you can call the command via class ProcessBuilder.

ProcessBuilder pb = new ProcessBuilder("sysctl", "-n", "machdep.cpu.brand_string");
try {
    Process p = pb.start();
    BufferedReader br = p.inputReader();
    String output = br.readLine();
    int status = p.waitFor();
    if (status == 0) {
        // Command succeeded.
    }
}
catch (InterruptedException | IOException x) {
    x.printStackTrace();
}

So you would probably want code similar to the following:

String details;
if ("Windows 10".equals(System.getProperty("os.name"))) {
    details = System.getenv("PROCESSOR_IDENTIFIER");
}
else if ("Mac OS X".equals(System.getProperty("os.name"))) {
    ProcessBuilder pb = new ProcessBuilder("sysctl", "-n", "machdep.cpu.brand_string");
    try {
        Process p = pb.start();
        BufferedReader br = p.inputReader();
        details = br.readLine();
        int status = p.waitFor();
        if (status == 0) {
            // Command succeeded.
        }
    }
    catch (InterruptedException | IOException x) {
        x.printStackTrace();
    }
}
like image 136
Abra Avatar answered Oct 22 '25 16:10

Abra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!