java_Runtime

来源:互联网 发布:linux安装软件命令yum 编辑:程序博客网 时间:2024/06/01 10:39

Rumtime类源码解析:

public class Runtime {    private static Runtime currentRuntime = new Runtime();    /**     * Returns the runtime object associated with the current Java application.     * Most of the methods of class <code>Runtime</code> are instance      * methods and must be invoked with respect to the current runtime object.      *      * @return  the <code>Runtime</code> object associated with the current     *          Java application.     */    public static Runtime getRuntime() { return currentRuntime;    }    /** Don't let anyone else instantiate this class */    private Runtime() {}}

以上只截取一部分源码,从以上代码可知,该类是一个单例,并且是恶汉模式的


下面介绍几个方法:

Runtime.getRuntime()

获取jvm运行环境,这是java中唯一一个得到运行环境的方法。

Runtime.exit()

退出当前jvm

Runtime.exec()

public Process exec(String command) throws IOException {return exec(command, null, null);}
 public Process exec(String command, String[] envp) throws IOException {        return exec(command, envp, null); }
public Process exec(String command, String[] envp, File dir)        throws IOException {        if (command.length() == 0)            throw new IllegalArgumentException("Empty command");StringTokenizer st = new StringTokenizer(command);String[] cmdarray = new String[st.countTokens()]; for (int i = 0; st.hasMoreTokens(); i++)    cmdarray[i] = st.nextToken();return exec(cmdarray, envp, dir);}
public Process exec(String cmdarray[]) throws IOException {return exec(cmdarray, null, null);}
 public Process exec(String[] cmdarray, String[] envp) throws IOException {return exec(cmdarray, envp, null); }
 public Process exec(String[] cmdarray, String[] envp, File dir)throws IOException {return new ProcessBuilder(cmdarray)    .environment(envp)    .directory(dir)    .start(); }
exec有多种重载方法,该方法主要用于执行系统命令

原创粉丝点击