代码之家  ›  专栏  ›  技术社区  ›  Alex Kokorin

无法使用ProcessBuilder运行shell脚本

  •  0
  • Alex Kokorin  · 技术社区  · 7 年前

        Process p = null;
        ProcessBuilder pb = new ProcessBuilder("scr.sh");
        pb.directory(new File("/Users/alex/"));
        p = pb.start();
        Thread.sleep(TimeConst.SECOND);
    

    并从中运行此代码 public static main() ,我确实在 scr.sh 在下面归档 alex 但收到异常的文件夹: Caused by: java.io.IOException: error=2, No such file or directory

    我的密码怎么了?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Alex Kokorin    7 年前

    我删除了一行指定的工作目录,并用绝对路径替换文件名,然后它就工作了。

    为了接收 echo 我必须从我的stdin中读到的信息(?):

    final Scanner in = new Scanner(p.getInputStream());
            new Thread(() -> {
                while (in.hasNextLine())
                    System.out.println(in.nextLine());
            }).start();