Remote Exit

As requested: How to remotely exit a program.

The program you want to exit needs some class that provides a remote interface. This interface should contain some method that will be implemented to stop the application. To make things nice you might want to program some Shutdownhooks and plug them into Runtime.getRuntime().addShutdownHook(…).

Here’s a sample “Exit” interface. Don’t forget all methods in a remote interface must throw a RemoteException.

public interface SatelliteWithExit extends Satellite {
    String REMOTE_NAME_EXIT="SatelliteWithExit";
    void exitServer() throws RemoteException;

Of course that interface must be implemented by some class. In my case I chose to implement this interface in my “main” class.
Don’t forget that your remote class must extend UnicastRemoteObject(I think there are other classes that can be extended to obtain an rmi object ….). You must start an rmi registry and bind your object there.

public class SatelliteWithExitServer extends UnicastRemoteObject implements SatelliteWithExit {

    public SatelliteWithExitServer() throws RemoteException {        super();            }

    private void startRegistry() throws RemoteException {    LocateRegistry.createRegistry(2323);    }

    public void exitServer() {   System.exit(1);    }

    public static void main(String[] args) {
        try {
            SatelliteWithExitServer server= new SatelliteWithExitServer();
            (new RmiSetup()).bind(args[0]+":2323",REMOTE_NAME_EXIT,server);               
        } catch (Exception e) {            

In this class you implement the method from the remote interface and call System.exit() or whatever else you want to trigger. Next you need a client that can obtain a reference to your remote object and call the exitServer() method.

public class ExitClient  {
  private SatelliteWithExit server;
  private void exitServer() throws RemoteException {      this.server.exitServer(); }
  private void getRmiServer(String rmihost,String remoteObject) throws Exception {
        this.server =  (SatelliteWithExit) Naming.lookup("//" + rmihost + "/"+remoteObject);
  public static void main(String[] args) {        
       TestClient testClient = new TestClient();

Before you implement any of this I suggest to check out the Java Rmi Tutorial and read up on security issues with rmi and some more!