Keyboard shortcut to restart Tomcat in Eclipse

When using Tomcat from Eclipse I often forget that one instance is already running. Then pressing CTRL+F11 again causes another launch. That causes this helpful message:

Eclipse error message

It says:

Several ports (8005, 8080, 8009) required by Tomcat v8.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).

Then you have to stop previous instance by red stop button. There is, as far as I know, no way to stop Tomcat by keyboard shortcut.

Also, when I need to restart Tomcat I need to stop it first by clicking on stop button. Then there is CTRL+F11 shortcut. Oh, well…

So I had to invent something. There is Launch group funcionality that seemed as viable path.

First, I have created following unittest. It tries to kill Tomcat server. It will work on Linux, for other systems you have to change processBuilder.command line. Also, there is simple check that this unittest is running on development environment - just checking that logged user is me. I am sure you can made up something smarter 😉

package com.navigo3.testing.sandbox;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;

import org.junit.Test;

import com.navigo3.utils.NavigoCollectiosUtils;

/**
 * This class is used for killing Tomcat server during development (Eclipse can run only Unittest from Launch group)
 */
public class KillServer {
	@Test
	public void testSample() {
		Set<String> allowedUsers = NavigoCollectiosUtils.asSet("jarek");
		
		if (!allowedUsers.contains(System.getProperty("user.name"))) {
			return;
		}
		
		ProcessBuilder processBuilder = new ProcessBuilder();
		processBuilder.command("/usr/bin/pkill", "-9", "-f", "org.apache.catalina.startup.Bootstrap");
		
		try {
			Process process = processBuilder.start();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			String line;
			while ((line = reader.readLine()) != null) {
				System.out.println(line + "\n");
			}

			int exitVal = process.waitFor();
			if (exitVal == 0) {
				System.out.println("Success!");
				System.exit(0);
			} else {
				System.out.println("Fail!");
				System.exit(-1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
}

Then just create unittest run configuration:

Unittest

I guess you already have your Tomcat configuration:

Tomcat config

And then combine Unittest and Tomcat into launch group (do not forget to set Wait until terminated). I use unittest because it is easiest way to launch some code from Launch group.

Launch group config

Press Run and voila! Before new instance of Tomcat is started the old one is killed.

Warning: It is quite a redneck hack that should not be used outside development environment.

Redneck hack

Tags:  java  tomcat  eclipse