Runtime类

java.lang.Runtime

每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。

应用程序不能创建自己的 Runtime 类实例。

Runtime是单例模式的

 1 public class Runtime {
 2     //初始时new对象,只此一个,本类私有
 3     private static Runtime currentRuntime = new Runtime();
 4 
 5     //给一个静态方法,获取这个Runtime对象
 6     public static Runtime getRuntime() { 
 7         return currentRuntime;
 8     }
 9 
10     //私有化构造方法
11     private Runtime() {}
12 
13     public void exit(int status) {
14         SecurityManager security = System.getSecurityManager();
15         if (security != null) {
16             security.checkExit(status);
17         }
18         Shutdown.exit(status);
19     }
20     //..........
21 
22 }

目前已知的应用:

执行其他程序

(Windows的扫雷,记事本。。各种应用)-----打开记事本

 1 import java.io.IOException;
 2 
 3 public class RunTimeDemo {
 4 
 5     /**
 6      * @param args
 7      * @throws IOException
 8      */
 9     public static void main(String[] args) throws IOException {
10         // TODO Auto-generated method stub
11         Runtime r = Runtime.getRuntime();
12         //public Process exec(String command)throws IOException
13         try {
14             r.exec("notepad");
15         } catch (IOException e) {
16             throw new IOException("Command is wrong!");
17         }
18     }
19 }

不光能打开记事本,还能让记事本干事儿,如同打开播放器,让播音乐,一个道理

1         Runtime r = Runtime.getRuntime();
2         // 用记事本打开这个java文件
3         r.exec("notepad.exe E:\\LearnJava\\Learn\\src\\specialobj\\RunTimeDemo.java");

杀死程序

能执行程序,当然也能杀死程序,不过只能直接杀死java开启的程序!!

1 try {
2     Process pros=r.exec("notepad.exe");
3     Thread.sleep(3000);
4     pros.destroy();
5     } catch (Exception e) {}

 然而,可以通过间接的形式--执行程序->程序杀死程序 的方式来达到杀死指定程序的目的--更多可能的操作,参考cmd命令taskkill /? ,tasklist /?

 1 import java.io.IOException;
 2 
 3 public class RunTimeDemo {
 4 
 5     /**
 6      * @param args
 7      * @throws IOException
 8      */
 9     public static void main(String[] args) throws IOException {
10 
11         Runtime r = Runtime.getRuntime();
12         //这个指令实际是Windows的dos命令执行
13         String cmd = "taskkill /F /IM EditPlus.exe";
14         // public Process exec(String command)throws IOException
15         try {
16             r.exec(cmd);
17         } catch (IOException e) {
18             throw new IOException("Command is wrong!");
19         }
20 
21     }
22 }

 

 

内存管理

后续研究

 

posted @ 2015-08-26 16:05  洱海  阅读(290)  评论(0编辑  收藏  举报
.First { margin: 10px 0; font-family: 'Microsoft Yahei'; text-align: left; padding: 6px 20px; color: #fff; background: #55895B; font-size: 20px; border-radius: 4px; clear: both; } .Second { margin: 10px 0; font-family: 'Microsoft Yahei'; padding: 6px 20px; background: #93C8A2; color: white; font-size: 18px; border-radius: 4px; clear: both; } .Third { margin: 10px 0; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0; font-size: 16px; color: black; background: #C6EFD2; border-radius: 4px; clear: both; } .note { margin: 10px 0; padding: 15px 20px 15px 60px; background: #FCFAA9 url('http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png') no-repeat 20px 0; font-size: 15px; font-family: 'Microsoft Yahei'; box-shadow: 0 0 8px #aaa; clear: both; } .demo { text-align: left; padding: 6px 20px; overflow: auto; border-radius: 4px; background: orange; color: #fff; font-size: 16px; clear: both; } .cnblogs_Highlighter { border: solid 1px #ccc; clear: both; } .cnblogs_code { background: #EFFFF4; border: solid 0px #939393; font-size: 14px; clear: both; padding: 10px 20px; } .cnblogs_code pre { font-size: 14px; } .cnblogs_code span { font-family: Courier New; font-size: 14px; }