Java, launch another application in the foreground

I want to run another application from Java code.

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

The process starts, but in the background. How to make it work in the foreground?

+5
source share
4 answers

You should tell cmd.exe that you want to open it in a new window:

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

ProcessC # waitFor () is what you are looking for.

+4
source

Run your command from JDialog and after running it use 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);
        }
    }
});
0
source

All Articles