The Runtime class is used to represent the state of the Java Virtual Machine when it is running, and it is used to encapsulate the Java Virtual Machine process. Each time the Java Virtual Machine is started with the “Java” command, there is a corresponding instance of Runtime, and only one instance, through which the application connects to its Runtime environment. An application cannot create its own Runtime instance. If you want to get an instance of Runtime in your application, you can get the associated Runtime object through the getRuntime() method as follows:

Runtime run = Runtime.getRuntime();

Because the Runtime class encapsulates the Java virtual machine process, you can retrieve information about the current virtual machine from an instance object of the class. Next, use a case to demonstrate the Runtime class, as shown in file 1.

public class Example12 { public static void main(String[] args) { Runtime rt = Runtime.getRuntime(); Println (” Number of processors: “+ rt.availableProcessors() +” one “); System.out.println(” Size of freeMemory: “+ rt.freememory () / 1024/1024 + “M”); Println (” maximum available memory size: “+ rt.maxMemory() / 1024/1024 + “M”); }}

The running results are shown in Figure 1:


In file 1, pass the “Runtime.getRuntime();” Method creates an instance of Runtime and calls availableProcessors(), freemory (), and maxMemory(), respectively, to print out the number of processors, freeMemory, and maximum available memory on the current virtual machine.

It is important to note that this file will print differently depending on the configuration and performance of each computer. In addition, both the free memory size and the maximum available memory size are calculated in bytes, and the results of the program in file 1 have been converted to a value in megabytes (M).

The Runtime class provides an exec() method that executes a DOS command for a game agent, achieving the same effect as typing a DOS command in a command-line window. For example, you can open a notepad that comes with Windows by running the command “Notepad. Exe” with the code shown in file 2.

import; public www.walajiao.comclass Example13 { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime(); // Create Runtime instance rt.exec(” Notepad. Exe “); // Call exec()}}

In file 2, the exec() method of the Runtime object is called and the system command “notepad. Exe” is passed as an argument to the method. After running the program, a Notepad opens on the desktop, as shown in Figure 2:


At this point, a new process, Notepad. Exe, is created on the Windows system and can be observed through the task manager, as shown in Figure 3.


The Runtime class’s exec() method returns a Process object that represents a Process on the operating system, Notepad. Exe, in this case. The Process object can be used to manage the Process that is created by calling destroy() to shut it down.

Next, use an example to implement the Notepad that opens and closes automatically after 3 seconds, as shown in file 3.

Public class Example14 {public static void main(String[] args) throws Exception Runtime.getRuntime(); Process = rt.exec(“notepad. Exe “); // The program 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. To highlight the demonstration, the static method sleep(long millis) of class Thread is used to make the program sleep for 3 seconds, so when the program runs, you will see the open Notepad automatically close after 3 seconds.