Java,在前台运行另一个应用程序

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

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

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

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

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

Process#waitFor()就是你要找的东西。

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

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

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