3

How do I determine the architecture of the system I'm currently in (x86, x86_64, aarch64, etc)?
I DO NOT want the JVM architecture (which System.getProperty("os.arch") gives).
I've already looked at this post, but the answers are all for windows (obviously), and the top answer's link does not work anymore.

7
  • 1
    Updated the accepted answer link with an archived page link. Commented Oct 3, 2022 at 14:48
  • It does not answer my question, as the used class is no longer accessible in Java 18 without shenanigans (or it has even been removed entirely). Commented Oct 3, 2022 at 14:55
  • The other question you mentioned is asking how to determine whether one is running 32 or 64-bit Windows. I'm asking for an OS independent solution of determining the system architecture. Commented Oct 3, 2022 at 15:09
  • By the way if you don't find build in Java method you always can write shell script and execute it in Java. Look this for more info: stackoverflow.com/questions/13707519/… Commented Oct 3, 2022 at 15:10
  • 1
    @ATP Note that accepted answer in the other question leads to native code example using IsWow64Process and Windows API docs for same say "Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows" Commented Oct 3, 2022 at 15:37

2 Answers 2

2

For non-Windows systems, you can use uname -m:

public static Optional<String> getSystemArchitecture() throws IOException, InterruptedException { String name = null; ProcessBuilder builder; if (System.getProperty("os.name").contains("Windows")) { builder = new ProcessBuilder("wmic", "os", "get", "OSArchitecture"); } else { builder = new ProcessBuilder("uname", "-m"); } builder.redirectError(ProcessBuilder.Redirect.INHERIT); Process process = builder.start(); try (BufferedReader output = new BufferedReader( new InputStreamReader( process.getInputStream(), Charset.defaultCharset()))) { String line; while ((line = output.readLine()) != null) { line = line.trim(); if (!line.isEmpty()) { name = line; } } } int exitCode = process.waitFor(); if (exitCode != 0) { throw new IOException( "Process " + builder.command() + " returned " + exitCode); } return Optional.ofNullable(name); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm looking for an OS-independent solution that does not rely on the os.name system property.
@illuminator3 I see. That definitely will be a challenge, since obtaining the architecture seems to be an OS-specific function.
1

The os.arch system property is Java's offering in this area (its description is literally "Operating system architecture"), but you say you don't want that. Java has no other built-in mechanism for determining that information.

2 Comments

Like I already said in my question, os.arch is the JVM architecture, not the system architecture.
And I already acknowledged that in this answer, @illuminator3. And as this answer also says, Java doesn't offer anything else (built in). I'm sorry that's not the answer you were hoping for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.