Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining system architecture

Tags:

java

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.

like image 370
illuminator3 Avatar asked Sep 15 '26 13:09

illuminator3


1 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);
}
like image 99
VGR Avatar answered Sep 17 '26 02:09

VGR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!