【问题标题】:Java, run another application in foregroundJava,在前台运行另一个应用程序
【发布时间】:2012-08-12 14:30:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: java process foreground


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      Process pr = rt.exec("cmd.exe /c start");
      

      【讨论】:

        【解决方案3】:

        Process#waitFor() 是你要找的。​​p>

        【讨论】:

          【解决方案4】:

          从 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);
                  }
              }
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-31
            • 1970-01-01
            • 2013-02-19
            • 2013-05-12
            • 1970-01-01
            • 2019-10-26
            • 2013-02-26
            相关资源
            最近更新 更多