Get OS-level system information

后端 未结 16 1983
情话喂你
情话喂你 2020-11-22 02:02

I\'m currently building a Java app that could end up being run on many different platforms, but primarily variants of Solaris, Linux and Windows.

Has anyone been abl

16条回答
  •  孤城傲影
    2020-11-22 02:24

    On Windows, you can run the systeminfo command and retrieves its output for instance with the following code:

    private static class WindowsSystemInformation
    {
        static String get() throws IOException
        {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("systeminfo");
            BufferedReader systemInformationReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
            StringBuilder stringBuilder = new StringBuilder();
            String line;
    
            while ((line = systemInformationReader.readLine()) != null)
            {
                stringBuilder.append(line);
                stringBuilder.append(System.lineSeparator());
            }
    
            return stringBuilder.toString().trim();
        }
    }
    

提交回复
热议问题