JavaFx防止客户端多次启动应用程序



我想防止客户端多次启动应用程序。我想过实现Singleton模式。

public class MyClass extends Application {
private static MyClass app = null;
private MyClass (){
super();   
}
public static MyClass getInstance(){
if(app == null){
app = new MyClass();
}
return app; 
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLLogin.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setOpacity(0.95);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

然而,当我运行该应用程序时,会出现一个错误:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class  authentification.MyClass
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: authentification.MyClass.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application authentification.MyClass

有人能解释一下原因吗?辛格尔顿的想法是错误的吗?感谢

JavaFX应用程序的生命周期由Application类控制。静态Application.launch(...)方法启动应用程序;它通过创建Application类的实例(默认情况下是从中调用launch()的类(,调用其无参数构造函数来实现。由于您的singleton方法将无参数构造函数设置为私有的,因此启动进程将无法再实例化Application类,并且会显示异常。

然而,即使没有这个问题,将应用程序类设为singleton也无法达到您想要的效果。这里使用的singleton模式只是确保在任何Java虚拟机中只能存在类的一个实例。如果用户第二次启动应用程序,他们将创建第二个JVM,该JVM可以拥有自己的单例实例。

为了确保一次只能运行一个应用程序,您需要一些操作系统级别的机制来锁定应用程序的启动。一种方法是在启动时开始侦听特定端口,并在关闭时关闭该连接。由于只有一个应用程序可以侦听给定的端口,因此可以达到所需的效果。有关示例,请参阅JavaFX单实例应用程序。

最新更新