runtime.exec()

简介:  Processexec(String command)           在单独的进程中执行指定的字符串命令。 Processexec(String[] cmdarray)           在单独的进程中执行指定命令和变量。 Processexec(String[] cmdarray, String[] envp)           在指定环境的独立



 Process exec(String command)
          在单独的进程中执行指定的字符串命令。
 Process exec(String[] cmdarray)
          在单独的进程中执行指定命令和变量。
 Process exec(String[] cmdarray, String[] envp)
          在指定环境的独立进程中执行指定命令和变量。
 Process exec(String[] cmdarray, String[] envp, File dir)
          在指定环境和工作目录的独立进程中执行指定的命令和变量。
 Process exec(String command, String[] envp)
          在指定环境的单独进程中执行指定的字符串命令。
 Process exec(String command, String[] envp, File dir)
          在有指定环境和工作目录的独立进程中执行指定的字符串命令。


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


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;


public class Runshell1 {

    public static void main(String[] args) {

        
        try {
           
        
           /* 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;


public class Runshell1 {

    public static void main(String[] args) {

        
        try {
            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

目录
相关文章
|
7月前
|
前端开发 Java
Java Process类Runtime.getRuntime().exec() 执行bat脚本程序
Java Process类Runtime.getRuntime().exec() 执行bat脚本程序
459 0
|
3月前
|
Unix Shell Linux
5-15|Docker报错OCI runtime exec failed: exec failed: unable to start container process: exec: “/bin/ba
5-15|Docker报错OCI runtime exec failed: exec failed: unable to start container process: exec: “/bin/ba
|
6月前
|
Unix Docker 容器
使用docker 启动naocs 报错出现:standard_init_linux.go:241: exec user process caused "exec format error"
```markdown Error in Docker container startup: "standard_init_linux.go:241: exec user process caused \"exec format error\"". Occurred at 2024-06-29 09:26:19.910, followed by a failed hook with a syslog delivery error at 09:27:20.193. Seeking solutions from experts. ```
|
7月前
|
Linux Shell Windows
Java.Runtime.exec()的使用
Java.Runtime.exec()的使用
35 0
|
Unix Java Linux
Runtime.exec方法之获取process id
Runtime.exec方法之获取process id
225 0
|
Java Shell Linux
java 调用外部程序(Runtime.getRuntime().exec)
java 调用外部程序(Runtime.getRuntime().exec)
1044 0
java 调用外部程序(Runtime.getRuntime().exec)
EXEC_PROGRAM
EXEC_PROGRAM
129 1
Runtime.getRuntime().exec()如何调用7z解压文件
Runtime.getRuntime().exec()如何调用7z解压文件
113 0
|
NoSQL MongoDB 数据安全/隐私保护
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown
1150 0
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown
|
Ubuntu Linux 应用服务中间件
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 报错)
3758 0