使用processBuilder执行shell命令并与之交互

时间:2014-01-21 09:10:18

标签: java shell process command processbuilder

我正在尝试创建一个允许我通过终端执行命令的程序(如果你想知道,这是用于覆盆子pi的OmxPlayer),但是我希望能够与它进行交互一旦我已经启动了命令。

例如我想做:omxplayer -win x1 y1 x2 y2 然后可以按“p”暂停视频/音频媒体

我已经有了一些可以用参数启动omxplayer的东西(实际上它是“ls”但它应该以完全相同的方式工作)但我不明白一旦我通过启动命令启动命令后如何与终端进行交互的ProcessBuilder。

这就是我现在所拥有的:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Main1 {

    public static void main(String a[]){

        InputStream is = null;
        ByteArrayOutputStream baos = null;
        List<String> commands = new ArrayList<String>();
        commands.add("ls");
        commands.add("-l");
        commands.add("/");
        ProcessBuilder pb = new ProcessBuilder(commands);
        try {
            Process prs = pb.start();
            is = prs.getInputStream();
            byte[] b = new byte[1024];
            int size = 0;
            baos = new ByteArrayOutputStream();
            while((size = is.read(b)) != -1){
                baos.write(b, 0, size);
            }
            System.out.println(new String(baos.toByteArray()));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        finally
        {
            try {
                if(is != null) is.close();
                if(baos != null) baos.close();
            } catch (Exception ex){}
        }
    }
}

1 个答案:

答案 0 :(得分:3)

“(实际上它是”ls“但它应该以完全相同的方式工作)”

不,不是。因为'ls'进程在调用后立即返回。另一方面,您的omixplayer是交互式的,并且会在运行时接受命令。

你需要做什么:

  • 创建一个实现Runnable的类,让这个类从prs.getInputStream()中读取。您将需要这个,因为.read()将阻止并等待新数据读取。

  • 获取Process对象的OutputStream(prs.getOutputStream())。您写入OutputStream的所有内容都将从您的omixplayer中读取。不要忘记刷新OutputStream,并且每个命令最后都需要“\ n”才能执行。

就像那样:

public class TestMain {
    public static void main(String a[]) throws InterruptedException {

        List<String> commands = new ArrayList<String>();
        commands.add("telnet");
        commands.add("www.google.com");
        commands.add("80");
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.redirectErrorStream(true);
        try {

            Process prs = pb.start();
            Thread inThread = new Thread(new In(prs.getInputStream()));
            inThread.start();
            Thread.sleep(2000);
            OutputStream writeTo = prs.getOutputStream();
            writeTo.write("oops\n".getBytes());
            writeTo.flush();
            writeTo.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class In implements Runnable {
    private InputStream is;

    public In(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {
        byte[] b = new byte[1024];
        int size = 0;
        try {
            while ((size = is.read(b)) != -1) {
                System.err.println(new String(b));
            }
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

P.S。:请记住这个例子很快就脏了。