2

I have a project in NetBeans that starts with a main function, that takes arguments. When I hit the "Stop" Button, the project continues running, but there is no output anymore. Currently I have to remember to manually stop the process from the console.

How can I modify my project, maven setup or NetBeans configuration to make the process halt when I hit stop.

I can live with the process not having its finalizers or ShutdownHooks called.

Consider this class:

public class Main {
    public static void main(String[] args) {
        System.out.println("args: " + Arrays.asList(args));
        new Main().action();
    }

    private void action() {
        Date start = new Date();
        long endTime = start.getTime() + 600000;
        Date end;
        do {
            synchronized (Thread.currentThread()) {
                try {
                    Thread.currentThread().wait(1000);
                    System.out.println("PING");
                } catch (InterruptedException iex) {
                    return;
                }
            }
            end = new Date();
        } while (end.getTime() < endTime);
    }
}

I want it to die using only NetBeans Stop button.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

1 Answers1

1

When you hit "Stop" in the NetBeans output, NB stops the maven process, but maven had spawned the actual program output, so the stop command is not handed over to your program.

NetBeans calls the exec-maven-plugin with the 'exec' goal by default. If you change it to the 'java' goal there is no new process. After the change below, the 'Run' section in your projects 'Properties' will be empty and of no use. I did not test what happens if you call 'System.exit' or similar in this scenario.

Also note, that you have to take care that you modify the version number here by yourself from now on.

You have to go to Projects window, right click your project, go to the Properties and select the Actions element and the the Run project entry. It will look like this:

Maven exec:exec in Netbeans

You have to modify

  1. Execute Goals: to process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:java (change the exec in the end to java).
  2. Set Properties: to exec.mainClass=de.steamnet.stopfromnetbeans.Main exec.classpath=%classpath exec.args=First Second 'Third Space'

So that it looks like this:

Maven exec:java in Netbeans

From now on, Maven will not fork a new process, so when you hit "Stop" it will actually Stop the process.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72