zoukankan      html  css  js  c++  java
  • 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 }

    内存管理

    后续研究

  • 相关阅读:
    面试笔记之手写单例模式
    Java学习笔记六:Java最基础
    Mac下终端工具iTerm2的快捷键
    Typescript引用类型--字符串
    Typescript引用类型--数组
    Typescript函数作用域
    Typescript的函数定义方式
    初识Typescript以及Typescript的安装
    VS Code前端开发常用插件和常用快捷键
    Vue CLI3项目构建
  • 原文地址:https://www.cnblogs.com/erhai/p/4760721.html
Copyright © 2011-2022 走看看