1.該類主要代表了應用程序的運行環境。一個RunTime就代表一個運行環境。
2.RunTime類常用的方法:
(1) getRuntime():該方法用於返回當前應用程序的運行環境對象。
(2) exec(String command):該方法用於根據指定的路徑執行對應的可執行文件。
①實例:
public class Demo7 {
public static void main(String[] args) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe");//打開記事本程序,並返回一個進程
Thread.sleep(3000); //讓當前程序停止3秒。
process.destroy();
}
}
②運行結果:
(3) freeMemory():該方法用於返回Java虛擬機中的空閒內存量,以字節爲單位。
(4) maxMemory():該方法用於返回Java虛擬機試圖使用的最大內存量。
(5) totalMemory():該方法用於返回Java虛擬機中的內存總量。
①實例:
public class Demo8 {
public static void main(String[] args) throws IOException{
Runtime runtime = Runtime.getRuntime();
System.out.println("Java虛擬機中的空閒內存量:"+runtime.freeMemory());
System.out.println("Java 虛擬機試圖使用的最大內存量:"+ runtime.maxMemory());
System.out.println("返回 Java 虛擬機中的內存總量:"+ runtime.totalMemory());
}
}
②運行結果: