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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With