Java obtains the number of computer CPU s and system information

Java obtains the number of computer CPU s and system information

Title Runtime class:
  • Each Java application has a Runtime class instance that enables the application to connect to its running environment. The application cannot create its own instance of the Runtime class. You can get the current Runtime through the getRuntime() method.
Properties class:
  • The properties class represents a persistent set of properties. Properties can be saved in or loaded from the stream. Each key and its corresponding value in the property list are a string. The list(PrintStream out) or list(PrintWriter out) method outputs the attribute list to the specified output stream.
System class:
  • The System class contains some useful class fields and methods. It cannot be instantiated. In the facilities provided by the System class, there are standard input, standard output and error output streams; Access to externally defined attributes and environment variables; Methods of loading files and libraries; There are also practical ways to quickly copy part of an array. The getProperties() method determines the current System properties.
import java.util.Properties;// Import Properties class
public class SystemInfo{
 public static void main(String args[]){
  Properties p=System.getProperties();// Get current system properties
  p.list(System.out);// Export attribute list
  System.out.print("CPU number:");//Runtime.getRuntime() gets the instance of the current runtime
  System.out.println(Runtime.getRuntime().availableProcessors());//availableProcessors() gets the current number of computer CPU s
  System.out.print("Total virtual machine memory:");
  System.out.println(Runtime.getRuntime().totalMemory());//totalMemory() gets the total amount of memory in the java virtual machine
  System.out.print("Amount of virtual machine free memory:");
  System.out.println(Runtime.getRuntime().freeMemory());//freeMemory() gets the amount of free memory in the java virtual machine
  System.out.print("Maximum amount of memory used by the virtual machine:");
  System.out.println(Runtime.getRuntime().maxMemory());//maxMemory() gets the maximum amount of memory that the java virtual machine is trying to use
  • The method is simple and practical. Although it is not often used, it is still necessary to understand it, because it is impossible to take out a dictionary every time. But the most important thing for opening up is to get the data and use it. We can't remember all the classes, methods and attributes, so we should be able to see them
    API and application.
  • In this example, to obtain System information, we need to use the Properties class. First, we need to create a Properties object, and then obtain the information of the current System through the getProperties() method of the System class. Then hand the obtained System Properties to the Properties object, which holds the collection of System information. Finally, the list method of the Properties class outputs the information. Several methods of the Runtime class can obtain other information accordingly. As long as we are familiar with these methods, we can naturally apply them.

The following describes system Getproperty system parameter:

Properties props=System.getProperties(); // System properties
System.out.println("Java Running environment version:"+props.getProperty("java.version"));
System.out.println("Java Operating environment supplier:"+props.getProperty("java.vendor"));
System.out.println("Java Supplier's URL: "+props.getProperty("java.vendor.url"));
System.out.println("Java Installation path:"+props.getProperty("java.home"));
System.out.println("Java Virtual machine specification version:"+props.getProperty("java.vm.specification.version"));
System.out.println("Java Virtual machine specification vendor:"+props.getProperty("java.vm.specification.vendor"));
System.out.println("Java Virtual machine specification Name:"+props.getProperty(""));
System.out.println("Java Virtual machine implementation version:"+props.getProperty("java.vm.version"));
System.out.println("Java Virtual machine implementation vendor:"+props.getProperty("java.vm.vendor"));
System.out.println("Java Virtual machine implementation name:"+props.getProperty(""));
System.out.println("Java Runtime environment specification version:"+props.getProperty("java.specification.version"));
System.out.println("Java Runtime environmental specification supplier:"+props.getProperty("java.specification.vender"));
System.out.println("Java Runtime environment specification Name:"+props.getProperty(""));
System.out.println("Java Class format version number of:"+props.getProperty("java.class.version"));
System.out.println("Java Classpath for:"+props.getProperty("java.class.path"));
System.out.println("List of paths searched when loading the Library:"+props.getProperty("java.library.path"));
System.out.println("Default temporary file path:"+props.getProperty(""));
System.out.println("Path to one or more extended directories:"+props.getProperty("java.ext.dirs"));
System.out.println("Name of operating system:"+props.getProperty(""));
System.out.println("Architecture of operating system:"+props.getProperty("os.arch"));
System.out.println("Operating system version:"+props.getProperty("os.version"));
System.out.println("File separator:"+props.getProperty("file.separator"));   // In unix system, it is "/"
System.out.println("Path separator:"+props.getProperty("path.separator"));   // In unix system, it is ":"
System.out.println("Row separator:"+props.getProperty("line.separator"));   // "In unix systems is" / n "
System.out.println("User's account name:"+props.getProperty(""));
System.out.println("User's home directory:"+props.getProperty("user.home"));
System.out.println("User's current working directory:"+props.getProperty("user.dir"));

Transferred from:

Tags: Java

Posted by randydg on Thu, 05 May 2022 13:25:40 +0300