簡體   English   中英

JavaFx 防止客戶端多次啟動應用程序

[英]JavaFx prevent client to launch application multiple times

我想防止客戶端多次啟動應用程序。 我想過實現 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

有人可以解釋我為什么嗎? Singleton 的想法是錯誤的嗎? 謝謝

JavaFX 應用程序的生命周期由Application class 控制。 static Application.launch(...)方法啟動應用程序; 它通過創建Application class 的實例(默認情況下是調用launch()的 class)的實例,調用其無參數構造函數來實現 由於您的 singleton 方法使無參數構造函數私有,因此啟動過程不再能夠實例化您的Application class,並且您會得到顯示的異常。

但是,即使沒有這個問題,使您的應用程序 class 成為 singleton 也無法實現您想要的。 您在此處使用的 singleton 模式僅確保 class 的一個實例可以存在於任何 Java 虛擬機中。 如果用戶第二次啟動應用程序,他們將創建第二個 JVM,它可以擁有自己的 singleton 實例。

為了確保一次只能運行一個應用程序,您需要一些操作系統級別的鎖定應用程序啟動的機制。 一種方法是在啟動時開始偵聽特定端口並在關閉時關閉該連接。 由於只有一個應用程序可以偵聽給定端口,因此可以達到預期的效果。 有關示例,請參閱JavaFX 單實例應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM