Java: If I have an instance of my program, how to detect it, and then close the old (s)

I need only one copy of my program. But I want him to close the old ones if they open the ores.

This is in Java.

+5
source share
5 answers

You can use the server socket address as an exclusive lock.

When your application starts, it will try to associate the server socket with a predefined address. If this fails, it means that the previous instance of the application is running and owns this address. Ping to instruct the owner to exit.

serverSocket = new ServerSocket(localhost:8888)
if success, 
    start ServerSocketListeningThread
else
    socket = new Socket(localhost:8888)
    socket.close();
    sleep(100);
    repeat attempt of binding server socket

ServerSocketListeningThread
    serverSocket.accept();  //block until someone connects
    System.exit(); 

Easy to disable the application from the command line

telnet localhost 8888
+1
source

Java Web Start, SingleInstanceService API JNLP. . SIS.

+2
+2

, java-, JVM. , :

  • RMI- ( )
  • cajo ( , , , )
  • unix/linux, shell script, .
  • , , , . , , , ( ), , JVM.
+2

Exiter.isAlone() main(). . ( Irreputable, ) 8181 ( ). - , jvm (System.exist()). u , . , , , . isAlone() . u (String [] s) , u , .

public class Exiter implements Runnable {

    private static final Exiter instance = new Exiter();
    private ServerSocket serverSocket;
    private int port = 8181;
    private boolean bStarting = true;

    private Exiter() {
        (new Thread(this)).start();
    }

    @Override
    public void run() {

        while (bStarting) {
            try {
                serverSocket = new ServerSocket(port);
                bStarting = false;
                awake();
            } catch (IOException e) {
                System.err.println("Exiter : "+e.getMessage()+"  port "+port);
                openSocketKillRequest();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
        try {
            Socket s = serverSocket.accept();// Ca bloque
            System.err.println("Kimll Request from :" + s.getRemoteSocketAddress());
            System.exit(0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void openSocketKillRequest() {
        try {
            System.err.println("Send request kill previous");
            Socket socket = new Socket("localhost", port);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Exiter getInstance() {
        return instance;
    }

    private synchronized void awake(){
        notifyAll();
    }

    public synchronized boolean isAlone() {
        if(bStarting){
            try {
                wait();
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
        return true;
    }
}
0

All Articles