使用Java检测系统规范

时间:2014-05-13 16:07:40

标签: java web system specs

有没有办法检测使用java的计算机的硬件规格?

示例:CPU,RAM,VGA,PCI设备,磁盘

感谢您的关注

2 个答案:

答案 0 :(得分:1)

您可以使用System#getProperties获取有关系统的大量信息。以及Runtime类。

东西清单:

我也躺在某处。

    public static void main(String[] args) {

    long kilobytes = 1024;
    long megabytes = kilobytes * 1024;
    long gigabytes = megabytes * 1024;

    String nameOS = "os.name";  
    String versionOS = "os.version";  
    String architectureOS = "os.arch";
    System.out.println("\n  Info about OS");
    System.out.println("\nName of the OS: " + 
    System.getProperty(nameOS));
    System.out.println("Version of the OS: " + 
    System.getProperty(versionOS));
    System.out.println("Architecture of The OS: " + 
    System.getProperty(architectureOS));
    Map<String, String> env = System.getenv();
    System.out.println("Environment values");
    for(String key : env.keySet()) {
        System.out.println("K: " + key + " \n\tV: " + env.get(key));
    }
    /* Total number of processors or cores available to the JVM */
    System.out.println("\nAvailable processors (cores): " + 
        Runtime.getRuntime().availableProcessors());

    /* Total amount of free memory available to the JVM */
    System.out.println("Free memory (megabytes): " + 
        Runtime.getRuntime().freeMemory() / (float) megabytes);

    /* This will return Long.MAX_VALUE if there is no preset limit */
    long maxMemory = Runtime.getRuntime().maxMemory();
    /* Maximum amount of memory the JVM will attempt to use */
    System.out.println("Maximum memory (megabytes): " + 
        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory / (float) megabytes));

    /* Total memory currently available to the JVM */
    System.out.println("Total memory available to JVM (megabytes): " + 
        Runtime.getRuntime().totalMemory() / (float) megabytes);

    /* Get a list of all filesystem roots on this system */
    File[] roots = File.listRoots();

    /* For each filesystem root, print some info */
    for (File root : roots) {
      System.out.println("\nFile system root: " + root.getAbsolutePath());
      System.out.println("Total space (gigabytes): " + (root.getTotalSpace() / (float) gigabytes));
      System.out.println("Free space (gigabytes): " + (root.getFreeSpace() / (float) gigabytes));
      System.out.println("Usable space (gigabytes): " + (root.getUsableSpace() / (float) gigabytes));

    }
    System.out.println("\n\nProperties:\n------\n");
    System.getProperties().list(System.out);
    }
    }

答案 1 :(得分:0)

为此,我认为您需要使用Java的Runtime类(我猜5)。

在应用程序的main()功能中使用它来获取与设备相关的操作系统和其他信息。

有关详情,请阅读:http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html