How does JavaFX 8 start the JavaFX Application thread in a nearly empty Application class?

自作多情 提交于 2019-12-03 12:50:29

Java uses different approaches to launch the two applications.

Try running the following code:

public class Test3 {

    public static void main(String[] args) {

        Class<?> actualMainClassTest = LauncherHelper.checkAndLoadMain(true, 1, Test.class.getName());
        Class<?> actualMainClassTest2 = LauncherHelper.checkAndLoadMain(true, 1, Test2.class.getName());

        System.out.println("Actual loaded main class for Test: " + actualMainClassTest.getName());
        System.out.println("Actual loaded main class for Test2: " + actualMainClassTest2.getName());
    }
}

The output is

  • Actual loaded main class for Test: sun.launcher.LauncherHelper$FXHelper
  • Actual loaded main class for Test2: Test2

You can see that the actual loaded main class for the Test2 class is Test2, but the loaded main class for Test is sun.launcher.LauncherHelper$FXHelper.

This happens because the Java launcher checks if the main class to be launched is a subclass of javafx.application.Application. If it is, it loads the main method of sun.launcher.LauncherHelper$FXHelper instead, which invokes a launcher method for JavaFX applications (com.sun.javafx.application.LauncherImpl#launchApplication).

This method is responsible for launching the JavaFX application. Test#main is called after the application is launched:

When Test#main is called by Test2, the FX launcher is not used because Test2 is not a subclass of javafx.application.Application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!