diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 4654655e860..4ba71bc68ef 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -131,19 +131,19 @@ private static void start(String[] args) { private static boolean handleMultipleAppInstances(String[] args) { RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences(); if (remotePreferences.useRemoteServer()) { - Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(), remotePreferences.getPort()); - - if (!Globals.REMOTE_LISTENER.isOpen()) { - // we are not alone, there is already a server out there, try to contact already running JabRef: - if (new RemoteClient(remotePreferences.getPort()).sendCommandLineArguments(args)) { - // We have successfully sent our command line options through the socket to another JabRef instance. + // Try to contact already running JabRef + RemoteClient remoteClient = new RemoteClient(remotePreferences.getPort()); + if (remoteClient.ping()) { + // We are not alone, there is already a server out there, send command line arguments to other instance + if (remoteClient.sendCommandLineArguments(args)) { // So we assume it's all taken care of, and quit. LOGGER.info(Localization.lang("Arguments passed on to running JabRef instance. Shutting down.")); return false; } + } else { + // We are alone, so we start the server + Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort()); } - // we are alone, we start the server - Globals.REMOTE_LISTENER.start(); } return true; } diff --git a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java index 19c4e350403..366f1a34334 100644 --- a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java +++ b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java @@ -129,4 +129,15 @@ void pingReturnsFalseForNoServerListening() throws IOException, InterruptedExcep assertFalse(new RemoteClient(port).ping()); } + + @Test + void pingReturnsTrueWhenServerIsRunning() { + final int port = 34567; + + try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { + server.openAndStart(messageHandler, port); + + assertTrue(new RemoteClient(port).ping()); + } + } }