I don't know if this is absolutely right, but here's the final code i've used and it works fine for me :
public class Loader {
private static final int PORT = 9999;
private static ServerSocket serverSocket = null; // Server
private static Socket socket = null; // CLient
private static final String focusProgram = "FOCUS";
public static void main(String[] args) {
if( !isProgramRunning() ) {
Main main = new Main();
main.setVisible( true );
}
else {
System.exit( 2 );
}
}
private static boolean isProgramRunning() {
try {
serverSocket = new ServerSocket(PORT,0,InetAddress.getByAddress(new byte[] {127,0,0,1})); // Bind to localhost adapter with a zero connection queue.
SwingWorker<String, Void> anotherThread = new SwingWorker<String, Void>() { // Do some code in another normal thread.
@Override
public String doInBackground() { // This method is to execute a long code in the other thread in background.
serverSocketListener();
return "";
}
};
anotherThread.execute(); // Execute the other tread.
}
catch (BindException e) {
System.err.println("Already running.");
clientSocketListener();
return true;
}
catch (IOException e) {
System.err.println("Unexpected error.");
e.printStackTrace();
return true;
}
return false;
}
public static void serverSocketListener() { // Server socket
try {
System.out.println( "Listener socket opened to prevent any other program instance." );
socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if( in.readLine().equals( focusProgram ) ) { // Restore the opened instance however you want.
Global.getCurrentFrame().setState(Frame.NORMAL);
Global.getCurrentFrame().toFront();
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void clientSocketListener() { // Client socket
try{
socket = new Socket(InetAddress.getByAddress( new byte[] {127,0,0,1}), PORT );
PrintWriter out = new PrintWriter( socket.getOutputStream(), true );
out.println( focusProgram );
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
}