1

我正在尝试通过 Java 在 linux 中执行终端命令,但我无法从 inputStream 获得任何输入。

这是我的代码

            ProcessBuilder build = new ProcessBuilder("/usr/bin/xterm", "find /home");

    Process pr = null;
            BufferedReader buf;
    try {
            build.redirectErrorStream(true);
            pr = build.start();
    buf = new BufferedReader(new InputStreamReader( pr.getInputStream()));

    String line = buf.readLine();
    pr.waitFor();
    while (true) {
    System.out.println(line + "sadasdas");
            line = buf.readLine();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

进程被执行并立即终端关闭,并且没有输出被捕获和打印。另一方面,如果我要编写一个未知的命令,我会得到所有提示如何使用命令的行。我在使用 Windows cmd 时遇到了同样的问题。我试图使用 getRuntime.exec(cmd) 方法,但结果是一样的。

我还尝试为看起来像这样的进程和阅读器创建单独的线程

public class kurdee
{
    public static Thread thread;
public kurdee()
{
List cmd = new LinkedList();
    cmd.add(new String("/usr/bin/xterm"));
    cmd.add(new String("find"));
    thisProc thispr = new thisProc(cmd);
    this.thread = new Thread(thispr);
    thread.start();
    reader rd = new reader(thispr.proc);
    Thread thread1 = new Thread(rd);
    thread1.start();}

public static void main(String args[]) 
{

    java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                kurdee kurd = new kurdee();

            }
        });





}
}
class reader implements Runnable
{

    private BufferedReader buf;
    private Process proc;
    public reader(Process proc)
    {
        this.proc=proc;
        this.buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    }

    public void run()
    {
                String line="";
                System.out.println("Thread is alive");
                try{
                    //Thread.sleep(1000);
        line = buf.readLine();
                }catch(Exception ex){System.out.println(ex + " before first while started");}
                while(kurdee.thread.isAlive())
                {
                    System.out.println("Thread is alive");

            while(line!=null)
            {
                                try{
                //System.out.println(proc.exitValue());
                System.out.println(line + " asd");
                line=buf.readLine();
                                }catch(Exception e){System.out.println(e + " Inner while loop");}
            }
                }
    }
}

class thisProc implements Runnable
{
    private ProcessBuilder build;
    public static Process proc=null;

    public thisProc(List<String> args)
    {
        this.build = new ProcessBuilder(args);
        build.redirectErrorStream(true);
                try{

                this.proc = build.start();
                }catch(Exception ex){System.out.println(ex + " proc class");}



    }

    public void run()
    {
         try{

                proc.waitFor();
                }catch(Exception ex){System.out.println(ex + " proc class");}       
    }
}

但是对于调用线程等的任何组合,我仍然没有什么可读的。

我正在尝试使用命令“find /home -xdev -samefile file”来获取文件的所有硬链接,所以也许有一种更简单的方法。

4

2 回答 2

2

xterm 不是在 unix 中执行进程的方式,它不是 shell。shell 类似于“/bin/sh”。但是,“find”是一个普通的 unix 可执行文件,所以你应该直接执行它,例如new ProcessBuilder("find", "/home"). 是的,您应该始终按照本文的建议在单独的线程上处理流。

于 2012-03-05T14:34:37.507 回答
1

首先,不要尝试用 执行命令xterm,那是没有意义的;直接做就行了。其次,当你组合命令字符串数组时要小心,在每个字符串中放入一个单词;传递,例如“find /home”作为许多 to 中的单个字符串ProcessBuilder将出错。

于 2012-03-05T14:36:45.050 回答