java_Runtime

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有多种重载方法,该方法主要用于执行系统命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值