Java 与 cmd 交互

Java interact with cmd

你好,对不起我的英语...

我必须在 windows 终端中使用 jframe 进行交互...

这是启动 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);
    }   

然后 jframe 在退出 cmd 行时启动...

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

如何?

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

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