Java与cmd交互

时间:2015-04-20 12:37:26

标签: java windows background cmd jframe

嗨抱歉我的英语......

我必须在带有jframe的Windows终端中进行交互......

这是启动cmd的代码

     try {

        String command = "file.exe";

        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec(command);

        input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        output = pr.getOutputStream();

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);

    } catch(IOException | InterruptedException e) {
        System.out.println(e.toString());
        e.printStackTrace(System.out);
    }   

然后当从cmd行退出时,jframe开始......

我需要在后台启动它并将输出传递给窗口......

如何?

1 个答案:

答案 0 :(得分:1)

也许你应该使用Threads: https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

然后,您将能够与流程实例进行交互。