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

Mee*_*egh 1 java singleton javafx javafx-11

我想防止客户端多次启动应用程序。我想到了实现单例模式。

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);
}
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行该应用程序时,出现错误:

 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
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下为什么吗?难道单例的想法是错误的吗?谢谢

Jam*_*s_D 5

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

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

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