美文网首页
Java 调用 shell 命令

Java 调用 shell 命令

作者: 李2牛 | 来源:发表于2018-05-10 21:34 被阅读0次

虽然说在一种语言里面东拉西扯地调用其他语言有些异类,但是不容质疑的是,每种语言都有其优势之处,扬长避短总是明智的选择。
shell 的大部分命令是用 C 语言实现的,所以二进制代码执行速度是 java 等高级语言无法望其项背的。
在 linux 平台下不能忘记强大的系统自带属性--shell.
之前用到的命令 sed,awk十分强大,在文本处理时可以从速度和操作便捷秒杀Java。
测试一下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author: kent
 * @date: 2018/5/10 21:11
 */
public class TestShell {
    public static void main(String[] args) {
        Process process;
        try {
            process = Runtime.getRuntime().exec("grep grep /Users/kent/.bash_history");//查看我的 .bash_history里面的grep 命令使用历史记录
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            int exitValue = process.waitFor();
            while((line = reader.readLine())!= null){
                System.out.println(line);
            }
            if (exitValue == 0){
                System.out.println( "successfully executed the linux command");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

调用还是比较便捷的。
来看一下JDK中的Runtime的源码

/**Runtime类型说明
 * Every Java application has a single instance of class
每一个Java应用都有一个允许应用与运行环境交互的Runtime单实例,当前的运行环境可以通过getRuntime方法获得
 * <code>Runtime</code> that allows the application to interface with
 * the environment in which the application is running. The current
 * runtime can be obtained from the <code>getRuntime</code> method.
 * <p>
 * An application cannot create its own instance of this class.
应用里面无法创建该类型的实例,后面可以发现构造方法被 private修饰了
 * @author  unascribed
 * @see     java.lang.Runtime#getRuntime()
 * @since   JDK1.0
 */
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() {}

最核心的运行方法 exec:

 /**
     * Executes the specified command and arguments in a separate process with
     * the specified environment and working directory.
     *在一个单独的Process里面运行声明环境和运行路径的指定命令和参数
     * <p>Given an array of strings <code>cmdarray</code>, representing the
     * tokens of a command line, and an array of strings <code>envp</code>,
     * representing "environment" variable settings, this method creates
     * a new process in which to execute the specified command.
     * 一组字符串cmdArray代表的是命令行,一组字符串envp代表环境变量,该方法可以创建一个运行指定命令的process
     * <p>This method checks that <code>cmdarray</code> is a valid operating
     * system command.  Which commands are valid is system-dependent,
     * but at the very least the command must be a non-empty list of
     * non-null strings.
     *该方法检查cmdArray 是明确的操作系统命令。至于什么样的命令是明确的则取决于操作系统。
     * <p>If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the
     * environment settings of the current process.
     *如果envp为空,子process继承当前process的环境设置
     * <p>A minimal set of system dependent environment variables may
     * be required to start a process on some operating systems.
     * As a result, the subprocess may inherit additional environment variable
     * settings beyond those in the specified environment.
     *
     * <p>{@link ProcessBuilder#start()} is now the preferred way to
     * start a process with a modified environment.
     *
     * <p>The working directory of the new subprocess is specified by <tt>dir</tt>.dir指定子Process的工作目录
     * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the
     * current working directory of the current process.
     *
     * <p>Starting an operating system process is highly system-dependent.
     * Among the many things that can go wrong are:
     * <ul>启动操作系统进程高度依赖于操作系统,所以以下地方可能出错
     * <li>The operating system program file was not found.操作系统程序未找到
     * <li>Access to the program file was denied.程序禁止访问
     * <li>The working directory does not exist.工作目录不存在
     * </ul>
     *
     * <p>In such cases an exception will be thrown.  The exact nature
     * of the exception is system-dependent, but it will always be a
     * subclass of {@link IOException}.
     *不同系统抛出异常的根本原因可能不同,但是异常总是IOException的子类
     *
     * @param   cmdarray  array containing the command to call and
     *                    its arguments.
     * 调用的命令以及其参数
     * @param   envp      array of strings, each element of which
     *                    has environment variable settings in the format
     *                    <i>name</i>=<i>value</i>, or
     *                    <tt>null</tt> if the subprocess should inherit
     *                    the environment of the current process.
     *环境变量,子进程会继承父进程的环境
     * @param   dir       the working directory of the subprocess, or
     *                    <tt>null</tt> if the subprocess should inherit
     *                    the working directory of the current process.
     *子进程工作目录
     * @return  A new {@link Process} object for managing the subprocess
     *
     * @throws  SecurityException
     *          If a security manager exists and its
     *          {@link SecurityManager#checkExec checkExec}
     *          method doesn't allow creation of the subprocess
     *
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @throws  NullPointerException
     *          If <code>cmdarray</code> is <code>null</code>,
     *          or one of the elements of <code>cmdarray</code> is <code>null</code>,命令为空以及环境变量为空导致空指针异常
     *          or one of the elements of <code>envp</code> is <code>null</code>
     *
     * @throws  IndexOutOfBoundsException
     *          If <code>cmdarray</code> is an empty array命令为空会导致数组越界异常
     *          (has length <code>0</code>)
     *
     * @see     ProcessBuilder
     * @since 1.3
     */
    public Process exec(String[] cmdarray, String[] envp, File dir)
        throws IOException {
        return new ProcessBuilder(cmdarray)
            .environment(envp)
            .directory(dir)
            .start();
    }

还有一些方法可以返回jvm的内存参数


这些方法比较底层

再看以下Process类型的源码
process是一个抽象类,用于创建一个子进程,执行系统命令。可以对接Runtime中的exec方法返回一个process实例。


image.png
这些抽象方法的具体实现取决于操作系统,不同的系统下其子类不同,unix平台下对应的是UNIXProcess类。
UNIXProcess类日后再行研究

相关文章

网友评论

      本文标题:Java 调用 shell 命令

      本文链接:https://www.haomeiwen.com/subject/yngtdftx.html