What can a Java virtual machine get?


The runtime class is used to represent the running state of the Java virtual machine. It is used to encapsulate the Java virtual machine process. Every time you use the “Java” command to start the Java virtual machine, there will be a runtime instance, and there is only one instance. The application will be connected to its runtime environment through this instance. The application cannot create its own runtime instance. If you want to obtain a runtime instance in the program, you can obtain the related runtime object through the getruntime () method. The specific methods are as follows:

  Runtime run = Runtime.getRuntime();

Because the runtime class encapsulates the Java virtual machine process, you can obtain the relevant information of the current virtual machine through the instance object of this class. Next, a case is used to demonstrate the use of the runtime class, as shown in file 1.

Public class example12 {public static void main (string [] args) {runtime RT = runtime. Getruntime(); / / get the runtime object system.out.println associated with the Java program (“number of processors:” + rt.availableprocessors() + “); system.out.println (” free memory size: “+ rt.freememory() / 1024 / 1024 +” m “); system.out.println( “Maximum available memory size:” + rt.maxmemory() / 1024 / 1024 + “m”);}}

The operation results are shown in Figure 1:


In file 1, through “runtime. Getruntime();” Method creates an instance object of runtime, and calls the availableprocessors() method, freememory() method and maxmemory() method of the object respectively to print out the number of processors, free memory size and maximum available memory size of the current virtual machine.

It should be noted that due to the different configuration and performance of each computer, the print results of this file will be different. In addition, the free memory size and the maximum available memory size are calculated in bytes. The running results of the program in file 1 have been converted into values in megabytes (m).

An exec () method is provided in the runtime class, which is used to execute aGame agentDOS command, so as to achieve the same effect as entering DOS command in the command line window. For example, you can open a notepad that comes with windows by running the “Notepad. Exe” command. The program code is shown in file 2.

import java. io. IOException; public www.walajiao. Comclass example13 {public static void main (string [] args) throws IOException {runtime RT = runtime. Getruntime(); / / create runtime instance object rt.exec (“Notepad. Exe”); / / call exec() method}}

In file 2, the exec () method of the Runtime object is invoked and the system command “notepad.exe” is passed to the method as a parameter. After running the program, a notepad will be opened on the desktop, as shown in Figure 2:


At this time, a new process notepad.com will be generated in the windows system Exe, which can be observed through the task manager, as shown in Figure 3.


The exec () method of the runtime class will return a process object that represents a process of the operating system, here Notepad Exe process. The new process generated can be managed through the process object. If you close this process, you only need to call the destroy () method.

Next, a case is used to realize the function of opening the notepad and automatically closing it after 3 seconds, as shown in file 3.

Public class example14 {public static void main (string [] args) throws exception {/ / create a runtime instance object runtime RT = runtime. Getruntime(); / / get the process object representing the process process process process process = rt.exec (“Notepad. Exe”); / / the process sleeps for 3 seconds thread.sleep (3000); / / close the process process. Destroy();}}

In file 3, the open Notepad is closed by calling the destroy () method of the process object. In order to highlight the effect of demonstration, the static method sleep (long millis) of thread class is used to make the program sleep for 3 seconds. Therefore, after the program runs, you will see that the open Notepad will close automatically after 3 seconds.