6

我想从 java 代码运行另一个应用程序。

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd.exe");

进程已启动,但在后台。如何让它在前台运行?

4

4 回答 4

5

Process#waitFor()是您正在寻找的。

于 2012-08-16T09:06:41.897 回答
4

您应该告诉 cmd.exe 您希望它在新窗口中打开:

Process pr = rt.exec("cmd.exe /c start");
于 2012-08-16T09:16:29.740 回答
1

在处理外部进程时考虑使用commons-exec 。在我看来,它比使用 Java Runtime 类更容易处理。

教程:http ://commons.apache.org/exec/tutorial.html

于 2012-08-16T09:11:36.090 回答
0

从 JDialog 运行命令,运行后使用 toBack()。

final JDialog dlg = new javax.swing.JDialog(null, "test", JDialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton button = new JButton("Select Me");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            java.awt.Desktop.getDesktop().open(
                    new java.io.File("/home/user/Downloads/jfreechart-1.0.13-US.pdf"));
            dlg.toBack();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    }
});
于 2012-08-16T09:05:12.010 回答