Java基础知识总结(三十二)--API--- java.lang.Runtime

时间:2025-02-03 07:00:47

类中没有构造方法,不能创建对象。

但是有非静态方法。说明该类中应该定义好了对象,并可以通过一个static方法获取这个对象。用这个对象来调用非静态方法。这个方法就是 static Runtime getRuntime();

这个Runtime其实使用单例设计模式进行设计。

class  RuntimeDemo {

public static void main(String[] args) throws Exception {

Runtime r = Runtime.getRuntime();

Process p = r.exec("notepad.exe SystemDemo.java"); //运行指定的程序

Thread.sleep(4000);

p.destroy();  //杀掉进程

}

}