Home > Blockchain >  Determining system architecture
Determining system architecture

Time:10-04

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.

CodePudding user response:

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);
}

CodePudding user response:

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.

CodePudding user response:

You can obtain more OS information using the OperatingSystemMXBean As per description, it Returns the operating system architecture.

OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
String arch = os.getArch();
  •  Tags:  
  • java
  • Related