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.
2 Answers
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); } 2 Comments
illuminator3
I'm looking for an OS-independent solution that does not rely on the os.name system property.
VGR
@illuminator3 I see. That definitely will be a challenge, since obtaining the architecture seems to be an OS-specific function.
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
illuminator3
Like I already said in my question,
os.arch is the JVM architecture, not the system architecture.John Bollinger
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.
IsWow64Processand 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"