Je ne sais pas si cela est tout à fait raison, mais voici le code final je l'ai utilisé et il fonctionne très bien pour moi:
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);
}
}
}
Je utilise BindException mais comment obtenir l'instance "frame"? – Brad
Je pense que l'idée ici est d'ouvrir un client socket à partir de la deuxième instance de l'application, connectez-le au serveur socket de la première instance d'application, et utilisez cette connexion pour dire à la première application de se mettre en avant. – jfpoilpret
Exactement 2ème application établit une connexion à la première application et envoie un message. La 1ère application connaît déjà son cadre et peut exécuter l'extrait fourni dans ma réponse. Vous ne pouvez pas énumérer les fenêtres de manière portable. – stacker