java进程,执行命令在Windows机器上不起作用

时间:2013-05-08 10:55:12

标签: process command outputstream

我正在通过java进程执行sbt。当它被执行时,我想与这个子进程(java)进行交互。例如,我想执行“show full-classpath”。这一切都适用于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机器上运行..

0 个答案:

没有答案
相关问题