2

我正在通过 java 进程执行 sbt。当它执行时,我想与这个子进程(java)进行交互。例如我想执行“显示完整的类路径”。这一切都适用于MAC并经过测试!但相同的源代码在 Windows 上不起作用!有人知道为什么吗?我还尝试通过附加“\n”、“\n\n”和“\r\n”来扩展命令。这是代码:

public Process makeProcess(String dir, String model) throws IOException {
        List<String> command = new ArrayList<String>();
        command.add("scala.bat");
        Process process = null;
        final ProcessBuilder pb = new ProcessBuilder();
        pb.command(command);

        pb.redirectErrorStream(true);

        process = pb.start();
        if (process == null) {
            return null;
        }
        final BufferedReader stream = new BufferedReader(new InputStreamReader(
                process.getInputStream(), Charset.defaultCharset()));

        OutputStream o = process.getOutputStream();

        Thread t = new Thread(new ConsoleListener(stream));
        t.start();



        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("executing....");
                o.write("show full-classpath\n".getBytes());

        o.flush();
                o.close();
        return process;
    }

    public static void main(String[] args) throws Exception {
        ProcessControl control = new ProcessControl();
        control.makeProcess(null,null);
    }

到目前为止我真的很困惑,因为当我使用“cmd.exe”和“dir”作为命令运行它时,它也可以在 Windows 机器上运行。

4

0 回答 0