Bi Xiangdong Teacher Java Basic Learning Note--runtime Object
After learning the runtime object in Java today, it is important for us to have the following useful objects.
1. Use Java code to open a local executable file, such as opening a calculator.
2. Open a program and use the program to open a supported file.
For example: 1. Open Notepad, open the *.java file with Notepad,
2. Open the Storm audio player and open a local video with the player.
The sample code is as follows:
/************************************** Runtime object: 1. The class does not provide a constructor function. The description cannot be a new object, then the methods in the class are directly thought to be static. 2. There is also a non-static method in this class stating that the class will certainly provide a way to get this class of object, and that the method is static, and that the return value type is of this class type. It can be seen from this feature that the class is completed using a singleton design pattern. The mode four static Runtime getruntime ();*/classruntimedemo{ Public Static voidMain (string[] args)throwsException {Runtime R=Runtime.getruntime (); Process P=r.exec ("Calc.exe");//Open Calculator Executable programThread.Sleep (4000);//process Hibernation 4000 msP.destroy ();//Kill the Calc.exe process, and note that you can only kill the process you have set up, not to kill the process that has been established. Process P2=r.exec ("notepad.exe D:/runtimedemo.java");//Open a Notepad program and open any type of file supported by Notepad. }}
Program:
2.Java Base Runtime Object