【问题标题】:opening a shell and interacting with its I/O in java在 java 中打开一个 shell 并与它的 I/O 交互
【发布时间】:2012-11-06 01:22:00
【问题描述】:

我正在尝试打开一个 shell (xterm) 并与之交互(编写命令并读取 shell 的输出)

这是一个不起作用的代码示例:

public static void main(String[] args) throws IOException {
    Process pr = new ProcessBuilder("xterm").start();
    PrintWriter pw = new PrintWriter(pr.getOutputStream());
    pw.println("ls");
    pw.flush();
    InputStreamReader in = new InputStreamReader(pr.getInputStream());
    System.out.println(in.read());
}

当我执行这个程序时,会打开一个“xterm”窗口并且没有输入“ls”命令。 只有当我关闭窗口时,我才会打印“-1”,并且不会从 shell 中读取任何内容

重要-

我知道我可以使用:
进程 pr = new ProcessBuilder("ls").start();

要获得输出,但我需要打开“xterm”以供其他用途

非常感谢

【问题讨论】:

  • 标题具有误导性。 xterm 是运行 shell 的终端应用程序。它本身不是一个外壳。

标签: java linux process terminal processbuilder


【解决方案1】:

您的问题是 xterm 进程的标准输入和输出与终端窗口中可见的实际 shell 不对应。与 xterm 相比,您可能会更成功地直接运行 shell 进程:

Process pr = new ProcessBuilder("sh").start();

【讨论】:

  • 我知道这已经有几年了,但其他人会看到。此解决方案本身不起作用。对于您想要工作的内容,您需要添加 -i 以使其具有交互性。在我意识到我应该只检查 /bin/sh pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html 的文档之前,我已经敲了一个小时的头
【解决方案2】:

Here 一个完整的 java 主要示例,说明如何在 java 8 上与 shell 进行交互(在 java 4、5、6 上这样做真的很简单)

输出示例

$ javac Main.java
$ java Main
echo "hi"
hi

代码

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {

        final List<String> commands = Arrays.asList("/bin/sh");
        final Process p = new ProcessBuilder(commands).start();

        // imprime erros
        new Thread(() -> {
            BufferedReader ir = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String line = null;
            try {
                while((line = ir.readLine()) != null){
                    System.out.printf(line);
                }
            } catch(IOException e) {}
        }).start();

        // imprime saida
        new Thread(() -> {
            BufferedReader ir = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            try {
                while((line = ir.readLine()) != null){
                    System.out.printf("%s\n", line);
                }
            } catch(IOException e) {}
        }).start();

        // imprime saida
        new Thread(() -> {
            int exitCode = 0;
            try {
                exitCode = p.waitFor();
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("Exited with code %d\n", exitCode);
        }).start();


        final Scanner sc = new Scanner(System.in);
        final BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        final String newLine = System.getProperty("line.separator");
        while(true){
            String c = sc.nextLine();
            bf.write(c);
            bf.newLine();
            bf.flush();
        }

    }

}

【讨论】:

    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多