3

(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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Andrei27
  • 43
  • 3
  • 3
    Evidently you're running the JVM under x86 emulation. – access violation Nov 04 '22 at 10:40
  • may be duplicate of https://stackoverflow.com/questions/1856565/how-do-you-determine-32-or-64-bit-architecture-of-windows-using-java#:~:text=Please%20note%2C%20the%20os.arch%20property%20will%20only%20give,you%20will%20need%20to%20write%20some%20native%20code. – Ashish Patil Nov 04 '22 at 10:54
  • Possible duplicate of [Get OS-level system information](https://stackoverflow.com/questions/25552/get-os-level-system-information) – Hovercraft Full Of Eels Nov 04 '22 at 11:04
  • Possible duplicate of [Detect CPU model information](https://stackoverflow.com/questions/55050864/detect-cpu-model-information) – Hovercraft Full Of Eels Nov 04 '22 at 11:04
  • Does this answer your question? [How do you determine 32 or 64 bit architecture of Windows using Java?](https://stackoverflow.com/questions/1856565/how-do-you-determine-32-or-64-bit-architecture-of-windows-using-java) – Mayuri S Kulkarni Nov 04 '22 at 11:38
  • 1
    It is not a duplicate. What you gave me I already searched and that gives you the architecture of the JRE you have. Because it is possible to have a jdk for intel processor on a m1 processor. I needed the type of processor, not how much cores, memory etc. it has. The answer is below :) Thanks for all the help and interest anyway. – Andrei27 Nov 04 '22 at 17:26

1 Answers1

2

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();
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Thanks a lot! This is exactly what I needed. "Apple M1" or "Intel" information. Did not know you can run process builders from Java :D – Andrei27 Nov 04 '22 at 17:27
  • @Andrei27 I just recently looked this up and there's tons of other cool info you can get. If I find the reference sheet I found on the web, I will post it here for future reference. – hfontanez Nov 04 '22 at 17:47
  • @hfontanez that would be awesome – Andrei27 Nov 06 '22 at 19:59
  • https://www.logicbig.com/how-to/code-snippets/jcode-java-system-getenv.html – hfontanez Nov 06 '22 at 21:17
  • @Andrei27 there was one better, but this is good enough – hfontanez Nov 06 '22 at 21:36
  • Besides `sysctl`, [other possibilities](https://stackoverflow.com/a/65259353/411282) that could be useful (some more general, e.g., if you want to treat M1,M2,M## the same in the future) are `uname` and `arch`. – Joshua Goldberg Jun 05 '23 at 15:15