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.
Asked
Active
Viewed 255 times
3

illuminator3
- 140
- 2
- 11
-
1Updated the accepted answer link with an archived page link. – ATP Oct 03 '22 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). – illuminator3 Oct 03 '22 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. – illuminator3 Oct 03 '22 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: https://stackoverflow.com/questions/13707519/running-a-bash-shell-script-in-java – Level_Up Oct 03 '22 at 15:10
-
There's probably no built-in function to give you what your looking for. You would probably have to drop down to executing shell commands to get the info. Here's a small demo I found doing a quick Google search: https://www.baeldung.com/run-shell-command-in-java – Mike Oct 03 '22 at 15:12
-
Another way is to make your own native c/c++ function to determinate, and run it using [JNI](https://www.baeldung.com/jni) like the accepted answer does – ATP Oct 03 '22 at 15:17
-
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" – DuncG Oct 03 '22 at 15:37
2 Answers
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);
}

VGR
- 40,506
- 4
- 48
- 63
-
I'm looking for an OS-independent solution that does not rely on the os.name system property. – illuminator3 Oct 03 '22 at 15:33
-
2@illuminator3 I see. That definitely will be a challenge, since obtaining the architecture seems to be an OS-specific function. – VGR Oct 03 '22 at 15:37
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.

John Bollinger
- 160,171
- 8
- 81
- 157
-
Like I already said in my question, `os.arch` is the **JVM architecture**, not the **system architecture**. – illuminator3 Oct 03 '22 at 15:01
-
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. – John Bollinger Oct 03 '22 at 15:50