steffen
steffen

Reputation: 17058

Eclipse: Terminate current process before re-running

I'm developing a small server application that uses an embedded jetty web server. The service binds to a TCP port and runs in the background. Since the application does not finish (it's a service) I have to terminate the process manually before I can re-run it (for example because the TCP port is in use by the current process).

Is it possible in Eclipse to terminate the current process automatically just before I relaunch it?

Upvotes: 3

Views: 3466

Answers (4)

ROMANIA_engineer
ROMANIA_engineer

Reputation: 56724

Eclipse Oxygen (4.7)

Eclipse Oxygen added the terminate and relaunch feature.

There are 2 possible approaches:

  1. Press Shift while clicking on the Run button.

  2. Go to Window > Preferences > Run/Debug > Launching > check Terminate and Relaunch while launching (Press 'Shift' to toggle during launch from menu or toolbar) > Apply and Close > run the application

    Run/Debug > Launching > Terminate and Relaunch

Upvotes: 5

jesse mcconnell
jesse mcconnell

Reputation: 7182

Jetty also has a ShutdownHandler that you can wire into your handler chain that will accept a connection from localhost with a secret key that can trigger a normal shutdown.

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ShutdownHandler.java

Alternately, you can wire up your own component with a reference to the Server object that just listens on another port for a stop key to be passed to it.

On the commandline, 'jps' is the most useful way to get a list of java processes.

Upvotes: 1

Jonas N
Jonas N

Reputation: 1777

Here's a snippet I wrote a year or two ago, and which has been doing it's job well ever since. Just add a package declaration. Call this from wherever you want to kill a process owning a port, such as your main().

import java.io.IOException;

public class KillPortHolderSimple {

    public static void killHolderOfPort(int port) {
        final String how = "-KILL";
        try {
            Runtime.getRuntime().exec(new String[]{"sh", "-c", "lsof -i :" + port + " -t | xargs kill " + how});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

LPD
LPD

Reputation: 2883

You can do it the code way, rather than using any eclipse config, which I fell better. When launching the application just check for the processes running in your system and search for the process that you want to launch. If you find that application running, send a kill signal to that and then continue with your launch. Hope that helps. And it works at all times, if you aren't using eclipse also. :P

Upvotes: 1

Related Questions