我想从java代码中运行另一个应用程序。
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd.exe");
流程已启动,但在后台。如何让它在前台运行?
答案 0 :(得分:4)
Process#waitFor()正是您要找的。 p>
答案 1 :(得分:3)
您应该告诉cmd.exe您希望它在新窗口中打开:
Process pr = rt.exec("cmd.exe /c start");
答案 2 :(得分:1)
在处理外部流程时考虑使用commons-exec。 在我看来,它比使用Java Runtime类更容易处理。
答案 3 :(得分: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);
}
}
});