虽然说在一种语言里面东拉西扯地调用其他语言有些异类,但是不容质疑的是,每种语言都有其优势之处,扬长避短总是明智的选择。

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方法获得
* Runtime that allows the application to interface with
* the environment in which the application is running. The current
* runtime can be obtained from the getRuntime method.
* 
* 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 Runtime are instance
* methods and must be invoked with respect to the current runtime object.
*返回当前运行环境
* @return the Runtime 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里面运行声明环境和运行路径的指定命令和参数
* 
Given an array of strings cmdarray, representing the
* tokens of a command line, and an array of strings envp,
* representing "environment" variable settings, this method creates
* a new process in which to execute the specified command.
* 一组字符串cmdArray代表的是命令行,一组字符串envp代表环境变量,该方法可以创建一个运行指定命令的process
* 
This method checks that cmdarray 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 是明确的操作系统命令。至于什么样的命令是明确的则取决于操作系统。
* 
If envp is null, the subprocess inherits the
* environment settings of the current process.
*如果envp为空,子process继承当前process的环境设置
* 
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.
*
* 
{@link ProcessBuilder#start()} is now the preferred way to
* start a process with a modified environment.
*
* 
The working directory of the new subprocess is specified by dir.dir指定子Process的工作目录
* If dir is null, the subprocess inherits the
* current working directory of the current process.
*
* 
Starting an operating system process is highly system-dependent.
* Among the many things that can go wrong are:
* 
• 启动操作系统进程高度依赖于操作系统,所以以下地方可能出错
* 
The operating system program file was not found.操作系统程序未找到 
 
* 
Access to the program file was denied.程序禁止访问 
 
* 
The working directory does not exist.工作目录不存在 
 
* 
*
* 
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
* name=value, or
* null
* the environment of the current process.
*环境变量,子进程会继承父进程的环境
* @param dir the working directory of the subprocess, or
* null
* 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 cmdarray is null,
* or one of the elements of cmdarray is null,命令为空以及环境变量为空导致空指针异常
* or one of the elements of envp is null
*
* @throws IndexOutOfBoundsException
* If cmdarray is an empty array命令为空会导致数组越界异常
* (has length 0)
*
* @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类日后再行研究