linux runtime pm机制的深入理解 https://blog.csdn.net/linux_devices_driver/article/details/38092115
Java 可以通过 Runtime 调用Linux命令 : Runtime.getRuntime().exec(command)
调用 Runtime.exec 方法将产生一个本地的进程,并返回一个Process子类的实例
(注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例)该实例可用于控制进程或取得进程的相关信息。
由于调用 Runtime.exec 方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)
都通过 Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream() 方法重定向给它的父进程了。
用户需要用这些stream来向子进程输入数据或获取子进程的输出,下面的代码可以取到 linux 命令的执行结果:
例1: JAVA利用Runtime执行py命令
public static void execPythonShell(File file) throws IOException {
String path = Utils.class.getClassLoader().getResource(“/import.py”).getPath();
Runtime runtime = Runtime.getRuntime();
BufferedReader br = null;
try {
// -c 意思是执行完成自动关闭,这里多条linux命令通过&&连接到一起
String[] cmds = new String[] {"/bin/sh","-c","cd " + path + "&&python edit_support_export_infor.py " + file.getPath() };
Process process = runtime.exec(cmds);
br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String tmp = null;
while ((tmp = br.readLine()) != null) {
System.out.println("Shell Message : " + tmp);
}
} catch (IOException e) {
logger.error(e);
} finally {
br.close();
}
}
例2: java调用process执行 shell 命令
public class ShellUtil {
public static String runShell(String shStr) throws Exception {
Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr});
process.waitFor();
BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
String result = "";
while ((line = read.readLine())!=null){
result+=line;
}
return result;//result 即为执行命令的输出
}
public static String runShellls(String shStr) throws Exception {
try {
String[] cmd = new String[]{”/bin/sh”, “-c”, ” ls “};
Process ps = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(”\n”);
}
String result = sb.toString();
System.out.println(result)
} catch (Exception e) {
}
}
}
注意:如果是windows操作系统要改为Runtime.getRuntime().exec(new String[]{"**cmd** exe","-c","command"});
例3. 当要执行多条时且不依赖事务,分开多次调用
public class ExecuteShell {
public static void main (String[] args){
String command1 = "some command";
String command2 = "some command";
String message1 = ShellUtil.runShell(command1);
String message2 = ShellUtil.runShell(command2);
System.out.println(message1);
System.out.println(message2);
}
}
但是当命令之间有事务依赖时,比如一条命令是登录数据库,第二条执行查询语句,上面分开多次调用的方式就不行。需要做改动如下
public class ExecuteShell {
public static void main (String[] args){
String command1 = "some command";
String command2 = "some command";
String command = command1 + " && " + command2;
String message = ShellUtil.runShell(command);
System.out.println(message);
}
}
例4. windows Runtime 执行多个cmd命令问题
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("sh");
DataOutputStream os = new DataOutputStream(proc.getOutputStream());
os.writeBytes("su\n");
os.writeBytes("./deviceinfo > /data/test.txt\n");
os.writeBytes("exit\n");
os.flush();