JavaFX 单实例应用程序
JavaFX Single Instance Application
试图在用户 "closes" 程序单击所有退出按钮时做到这一点,因此不再有托盘图标。
我调用了Platform.setImplicitExit(false);所以程序仍然在后台运行。
我正在尝试学习如何制作它,以便当用户重新单击运行 jar 的 .exe 文件而不是 运行 一个新程序时,它会重新显示 运行 在后台。
Platform.setImplicitExit(false);
这是基于博客 post 中的解决方案:Java Single Instance Application。
解决方案使用“套接字技术”:
With this technique we start listening on a port, only one process can listen on a socket so after first instance of our application binds itself to the socket other instances will get BindException, which means we are already running.
Cons of this approach is that some virus scanners will give a warning when an application starts listening on a socket, depending on your user base this could be interpreted badly. You should pick a port number thats not commonly used and high or you won't even get a single instance of your application running.
在示例中,我们为应用程序实例创建了一个唯一的实例ID,并记录了一些选项。
- Minimize 将最小化 window.
- 隐藏 将隐藏它(因此它不会显示为最小化,但应用程序仍然 运行)。
- 退出将结束申请流程。
window 上的 OS 关闭按钮将关闭应用程序 window,但应用程序进程将在后台继续 运行(所以它的作用相同作为“隐藏”按钮)。
当您启动一个应用程序实例时,它会打开一个套接字并监听它。
当您尝试启动另一个应用程序实例时,它会尝试绑定到侦听套接字。如果它无法绑定,那么它知道该套接字上已经有一个应用程序实例 运行。如果检测到另一个实例,则会通过套接字向现有实例发送一条消息,导致现有实例取消隐藏或取消最小化自身,并尝试将其舞台置于最前面。
请不要滥用这个,有很多我不喜欢的隐藏在后台的程序。
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
public class SingleInstanceApp extends Application {
private static final int SINGLE_INSTANCE_LISTENER_PORT = 9999;
private static final String SINGLE_INSTANCE_FOCUS_MESSAGE = "focus";
private static final String instanceId = UUID.randomUUID().toString();
// We define a pause before focusing on an existing instance
// because sometimes the command line or window launching the instance
// might take focus back after the second instance execution complete
// so we introduce a slight delay before focusing on the original window
// so that the original window can retain focus.
private static final int FOCUS_REQUEST_PAUSE_MILLIS = 500;
private Stage stage;
public void init() {
CountDownLatch instanceCheckLatch = new CountDownLatch(1);
Thread instanceListener = new Thread(() -> {
try (ServerSocket serverSocket = new ServerSocket(SINGLE_INSTANCE_LISTENER_PORT, 10)) {
instanceCheckLatch.countDown();
while (true) {
try (
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()))
) {
String input = in.readLine();
System.out.println("Received single instance listener message: " + input);
if (input.startsWith(SINGLE_INSTANCE_FOCUS_MESSAGE) && stage != null) {
Thread.sleep(FOCUS_REQUEST_PAUSE_MILLIS);
Platform.runLater(() -> {
System.out.println("To front " + instanceId);
stage.setIconified(false);
stage.show();
stage.toFront();
});
}
} catch (IOException e) {
System.out.println("Single instance listener unable to process focus message from client");
e.printStackTrace();
}
}
} catch(java.net.BindException b) {
System.out.println("SingleInstanceApp already running");
try (
Socket clientSocket = new Socket(InetAddress.getLocalHost(), SINGLE_INSTANCE_LISTENER_PORT);
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()))
) {
System.out.println("Requesting existing app to focus");
out.println(SINGLE_INSTANCE_FOCUS_MESSAGE + " requested by " + instanceId);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Aborting execution for instance " + instanceId);
Platform.exit();
} catch(Exception e) {
System.out.println(e.toString());
} finally {
instanceCheckLatch.countDown();
}
}, "instance-listener");
instanceListener.setDaemon(true);
instanceListener.start();
try {
instanceCheckLatch.await();
} catch (InterruptedException e) {
Thread.interrupted();
}
}
public void stop() {
System.out.println("Exiting instance " + instanceId);
}
@Override
public void start(Stage stage) throws Exception{
this.stage = stage;
System.out.println("Starting instance " + instanceId);
Platform.setImplicitExit(false);
Button minimize = new Button("Minimize");
minimize.setOnAction(event -> stage.setIconified(true));
Button hide = new Button("Hide");
hide.setOnAction(event -> stage.hide());
Button exit = new Button("Exit");
exit.setOnAction(event -> Platform.exit());
Label instance = new Label(instanceId);
Pane layout = new VBox(10, instance, new HBox(10, minimize, hide, exit));
layout.setPadding(new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
相关问题
如果此答案中提供的解决方案的变体和可能的替代方法包含进一步讨论:
试图在用户 "closes" 程序单击所有退出按钮时做到这一点,因此不再有托盘图标。
我调用了Platform.setImplicitExit(false);所以程序仍然在后台运行。
我正在尝试学习如何制作它,以便当用户重新单击运行 jar 的 .exe 文件而不是 运行 一个新程序时,它会重新显示 运行 在后台。
Platform.setImplicitExit(false);
这是基于博客 post 中的解决方案:Java Single Instance Application。
解决方案使用“套接字技术”:
With this technique we start listening on a port, only one process can listen on a socket so after first instance of our application binds itself to the socket other instances will get BindException, which means we are already running.
Cons of this approach is that some virus scanners will give a warning when an application starts listening on a socket, depending on your user base this could be interpreted badly. You should pick a port number thats not commonly used and high or you won't even get a single instance of your application running.
在示例中,我们为应用程序实例创建了一个唯一的实例ID,并记录了一些选项。
- Minimize 将最小化 window.
- 隐藏 将隐藏它(因此它不会显示为最小化,但应用程序仍然 运行)。
- 退出将结束申请流程。
window 上的 OS 关闭按钮将关闭应用程序 window,但应用程序进程将在后台继续 运行(所以它的作用相同作为“隐藏”按钮)。
当您启动一个应用程序实例时,它会打开一个套接字并监听它。
当您尝试启动另一个应用程序实例时,它会尝试绑定到侦听套接字。如果它无法绑定,那么它知道该套接字上已经有一个应用程序实例 运行。如果检测到另一个实例,则会通过套接字向现有实例发送一条消息,导致现有实例取消隐藏或取消最小化自身,并尝试将其舞台置于最前面。
请不要滥用这个,有很多我不喜欢的隐藏在后台的程序。
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
public class SingleInstanceApp extends Application {
private static final int SINGLE_INSTANCE_LISTENER_PORT = 9999;
private static final String SINGLE_INSTANCE_FOCUS_MESSAGE = "focus";
private static final String instanceId = UUID.randomUUID().toString();
// We define a pause before focusing on an existing instance
// because sometimes the command line or window launching the instance
// might take focus back after the second instance execution complete
// so we introduce a slight delay before focusing on the original window
// so that the original window can retain focus.
private static final int FOCUS_REQUEST_PAUSE_MILLIS = 500;
private Stage stage;
public void init() {
CountDownLatch instanceCheckLatch = new CountDownLatch(1);
Thread instanceListener = new Thread(() -> {
try (ServerSocket serverSocket = new ServerSocket(SINGLE_INSTANCE_LISTENER_PORT, 10)) {
instanceCheckLatch.countDown();
while (true) {
try (
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()))
) {
String input = in.readLine();
System.out.println("Received single instance listener message: " + input);
if (input.startsWith(SINGLE_INSTANCE_FOCUS_MESSAGE) && stage != null) {
Thread.sleep(FOCUS_REQUEST_PAUSE_MILLIS);
Platform.runLater(() -> {
System.out.println("To front " + instanceId);
stage.setIconified(false);
stage.show();
stage.toFront();
});
}
} catch (IOException e) {
System.out.println("Single instance listener unable to process focus message from client");
e.printStackTrace();
}
}
} catch(java.net.BindException b) {
System.out.println("SingleInstanceApp already running");
try (
Socket clientSocket = new Socket(InetAddress.getLocalHost(), SINGLE_INSTANCE_LISTENER_PORT);
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()))
) {
System.out.println("Requesting existing app to focus");
out.println(SINGLE_INSTANCE_FOCUS_MESSAGE + " requested by " + instanceId);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Aborting execution for instance " + instanceId);
Platform.exit();
} catch(Exception e) {
System.out.println(e.toString());
} finally {
instanceCheckLatch.countDown();
}
}, "instance-listener");
instanceListener.setDaemon(true);
instanceListener.start();
try {
instanceCheckLatch.await();
} catch (InterruptedException e) {
Thread.interrupted();
}
}
public void stop() {
System.out.println("Exiting instance " + instanceId);
}
@Override
public void start(Stage stage) throws Exception{
this.stage = stage;
System.out.println("Starting instance " + instanceId);
Platform.setImplicitExit(false);
Button minimize = new Button("Minimize");
minimize.setOnAction(event -> stage.setIconified(true));
Button hide = new Button("Hide");
hide.setOnAction(event -> stage.hide());
Button exit = new Button("Exit");
exit.setOnAction(event -> Platform.exit());
Label instance = new Label(instanceId);
Pane layout = new VBox(10, instance, new HBox(10, minimize, hide, exit));
layout.setPadding(new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
相关问题
如果此答案中提供的解决方案的变体和可能的替代方法包含进一步讨论: