1

I have a Java program compiled in a .jar, so the end user can't really just ctrl+c it in the console.

They have to end the java process in the task manager.
However, there is a much simpler way, isn't there?

public class Test extends JFrame {
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);

        JButton go = new JButton("Go");
        go.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Process p;
                Runtime r = Runtime.getRuntime();
                while(true) {
                    try {
                        p = r.exec("notepad.exe");
                        p.waitFor();
                    } catch (IOException | InterruptedException e1) {
                        e1.printStackTrace();
                    }

                }
            }
        });
        contentPane.add(go);
    }
}

As you can see, all it does, once you press the Go button, is spawn a notepad process.
As soon as you close notepad, it spawns another one. I want it to do that.

However, there's no way to stop it halting. For example, pressing the X on the pane doesn't do anything.

How do I make it so that the X effectively closes the Java program, while keeping all the contingencies above?

user2837858
  • 339
  • 6
  • 18
  • Could you get the process id of the notepad instance that was started, and periodically see if it was closed? I think it would help other if you noted whether this is being designed for a specific OS, as maybe there are some OS specific tricks you can do. – thatidiotguy Oct 22 '14 at 20:50
  • I guess my program is only designated for Windows, then – user2837858 Oct 22 '14 at 20:51
  • So you want your program to both open a new notepad and not open a new one when the current one is closed? I'm confused... (why not create a "quit" button next to your "go" button? And do the `waitFor` in a different thread.) – vanza Oct 22 '14 at 20:51
  • You can add another JButton that quits out the program – ryekayo Oct 22 '14 at 20:52
  • The thing is that `p.waitFor()` effective stops the Java program from being able to fire off another listener. – user2837858 Oct 22 '14 at 20:53

1 Answers1

0

1) the action performed is running on the EDT thread: the java thread executing all events/event handlers. As p.waitFor does not return immediately it will block all futher event handling

2) one should never run long running actions on the edt thread. In this case I suggest to spwan a new thread that will start the notepad.exe and wait for it in a different thread...

3) another point is: why do you want to wait for the notepad.exe to exit ? There is some subtle inconsistency here, from one perspective you want the application to continue normal processing (clicking on the x box should exit the application) and on the other hand you want your application not to continue normal processing as you wish to wait for the notepad to exit...

explain your contigencies a bit better

GerritCap
  • 1,606
  • 10
  • 9