zl程序教程

您现在的位置是:首页 >  其它

当前栏目

runtime.exec()

runtime exec
2023-09-14 09:00:23 时间


dir 指定了新子进程的工作目录。如果 dir 为 null,那么子进程会继承当前进程的当前工作目录


waitFor导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,0 表示正常终止。


wKiom1YQrH2AsvzFAAB0v4GqoLo557.jpg

一些平台只为标准输入输出提供有限的缓存。错误的写子进程的输入流或者错误的都子进程的输出流都有可能造成子进程的阻塞,甚至是死锁。


getErrorStream得到错误信息

getInputStream   得到正常信息




Runtime.exec() 不等同于直接执行command line命令!

Runtime.exec()很有局限性,对有些命令不能直接把command line里的内容当作String参数传给exec().

比如重定向等命令。举个例子:

javap -l xxx output.txt


这时要用到exec的第二种重载

把整个命令都当成/bin/sh的参数传入


linux下

Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","javap -l xxx output.txt"});

windows下

Process p = Runtime.getRuntime().exec(new String[]{"cmd","/c","javap -l xxx output.txt"});


linux执行shell脚本(带参数)

1

假设有一个shell脚本文件test.sh,有两个参数parm1,parm2,java调用的方法如下:
String[] cmd = {"/bin/sh","-c","test.sh parm1 parm2"};
Runtime.getRuntime().exec(cmd);

2.

不带参数

String PATH = "/XXXXX/a.sh";

        try {

            Process num = Runtime.getRuntime().exec(PATH);

        } catch (IOException e) {

            e.printStackTrace();

        }

带参数传入数组

String[] PATH = {"/XXXXX/a.sh","param1","param2"};

        try {

            Process num = Runtime.getRuntime().exec(PATH);

        } catch (IOException e) {

            e.printStackTrace();

        }
windows下代码(需转码)
package com.zxing.imgQRCode;

import java.io.BufferedReader;

import java.io.InputStreamReader;


           /* Process p = Runtime.getRuntime().exec(                     "D:/Program Files/Wireshark/Wireshark.exe");*/             Process p = Runtime.getRuntime().exec(                     new String[]{"cmd","/c","dir"});             p.waitFor();             InputStreamReader in = new InputStreamReader(                     p.getInputStream(),"GBK");             BufferedReader br = new BufferedReader(in);             String lineStr;             while ((lineStr = br.readLine()) != null) {                 System.out.println(lineStr);             }         } catch (Exception e) {             e.printStackTrace();         }     } }
linux下代码
package com.zxing.imgQRCode;

import java.io.BufferedReader;

import java.io.InputStreamReader;


            Process p = Runtime.getRuntime().exec(                     new String[] { "/bin/sh", "-c", "ls  test.txt"}, null, null);                      /*Process p = Runtime.getRuntime().exec(                     "D:/Program Files/Wireshark/Wireshark.exe");*/             Process p = Runtime.getRuntime().exec(                     new String[]{"cmd","/c","dir"});*/             p.waitFor();             InputStreamReader in = new InputStreamReader(                     p.getInputStream());             BufferedReader br = new BufferedReader(in);             String lineStr;             while ((lineStr = br.readLine()) != null) {                 System.out.println(lineStr);             }         } catch (Exception e) {             e.printStackTrace();         }     } }
Java执行带重定向或管道的shell命令的问题

http://www.linuxidc.com/Linux/2012-07/64526.htm



本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1700098


Runtime的使用 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的。
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: ip : executable file not found in $PATH: unknown (Docker容器没有ip addr命令:exec ip addr 报错) OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: ip : executable file not found in $PATH: unknown (Docker容器没有ip addr命令:exec ip addr 报错)