0

我正在尝试使用 java + 应用代码打开 CMD 以打开 .jar,以便应用程序输出显示在 .bat 文件中。有人可以告诉我该怎么做吗?

这是它得到的代码,它运行执行文件但 CMD 不显示。

btnTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String Bat = "C:"+File.separatorChar+"Users"+File.separatorChar+"Gebruiker"+File.separatorChar+"AppData"+File.separatorChar+"Local"+File.separatorChar+"Temp"+File.separatorChar+"hexT"+File.separatorChar+"run.bat";
            Runtime rt = Runtime.getRuntime();
            try {
                rt.exec(Bat);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
4

2 回答 2

0

定义这个:

FileWriter writer;

然后在您的 try/catch 中执行以下操作:

    try {
        writer = new FileWriter("test.txt");
        Process child = rt.exec(Bat);
        InputStream input = child.getInputStream();
        BufferedInputStream buffer = new BufferedInputStream(input);
        BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
        String line = "";
        try {
            while ((line = commandResult.readLine()) != null) {
         writer.write(line + "\n");              
            }
        writer.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这将逐行读取输出作为缓冲区并将其写入文本文件

于 2013-09-04T06:12:40.560 回答
0

编辑:这对我有用:

String Bat = "C:\\app.bat";  //Try to use \\ as path seperator

try {
    Runtime.getRuntime().exec("cmd /c start " + Bat);
} catch (IOException e) {
    e.printStackTrace();
}
于 2013-09-03T19:38:25.383 回答