黑马程序员——Java基础——其他对象(Runtime)

来源:互联网 发布:笨办法学python百度云 编辑:程序博客网 时间:2024/06/09 01:47
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


<span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
 * Runtime类:1.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的系统环境相连接。2.应用程序不能创建自己的 Runtime 类实例3.可以通过static Runtime getRuntime(); 方法获取该对象。 4.由 2和3 两点可以看出该类使用了单例设计模式完成。 */public class RuntimeDemo {public static void main(String[] args)throws Exception{//该类的一些方法的使用://1.打开程序如:打开c盘下的扫雷游戏,将扫雷程序复制到C盘根目录下Runtime r = Runtime.getRuntime();r.exec("c:\\winmine.exe");//如果不写完整路径,则会先查找在环境变量中的path变量配置的路径中查找,//如果没有则报java.io.IOException: Cannot run program异常r.exec("winmine.exe");//2.用程序打开文件并获取该进程如:用notepad.exe 程序打开 test.txt文件Process process = r.exec("notepad.exe C:\\test.txt");//让当前正在执行的线程休眠4秒Thread.sleep(4000);//3.用获取到的进程对象结束用该类方法启动的进程获取线程对象process.destroy(); }}

0 0
原创粉丝点击