9

在 Java 8 中,我可以使用以下方法启动带有预加载器的 JavaFX 应用程序:

LauncherImpl.launchApplication(WindowMain.class, WindowMainPreloader.class, 
new String[]{...});

我更喜欢像上面那样从代码启动它,而不是使用部署配置,因为我不希望每次启动应用程序时都启动图形界面,但只有在计算出应用程序应该运行的一些代码之后图形用户界面模式。

我使用的是“com.sun.javafx.application.LauncherImpl”类,但显然在 Java 9 中,所有以“com.sun”开头的类都被删除了。那么,如何在 Java 9 中使用预加载器启动应用程序?

4

3 回答 3

11

对这个问题的答案有评论:

如何在 JavaFX 独立应用程序中创建启动画面作为预加载器?

系统属性javafx.preloader=classname似乎也有效。

我没有尝试过,但也许您可以尝试设置该属性并通过公共Application.launch(appClass, args)API 启动您的主应用程序,也许预加载器将首先启动。

查看 的代码Application.launch,这似乎可行。下面是最终调用的代码,从 Java 8 源代码复制而来:

public static void launchApplication(final Class<? extends Application> appClass,
        final String[] args) {

    Class<? extends Preloader> preloaderClass = savedPreloaderClass;

    if (preloaderClass == null) {
        String preloaderByProperty = AccessController.doPrivileged((PrivilegedAction<String>) () ->
                System.getProperty("javafx.preloader"));
        if (preloaderByProperty != null) {
            try {
                preloaderClass = (Class<? extends Preloader>) Class.forName(preloaderByProperty,
                        false, appClass.getClassLoader());
            } catch (Exception e) {
                System.err.printf("Could not load preloader class '" + preloaderByProperty +
                        "', continuing without preloader.");
                e.printStackTrace();
            }
        }
    }

    launchApplication(appClass, preloaderClass, args);
}

因此,您应该能够使用以下方式启动带有预加载器的应用程序:

System.setProperty("javafx.preloader", "my fully qualified preloader class name");
Application.launch(myMainClass, args);
于 2017-11-28T20:07:47.667 回答
3

从 jdk 9 开始,LauncherImpl 不起作用jdk 10 - java.graphics module-info.java

包中的所有类都com.sun.javafx.application导出到特殊模块(java.base,javafx.controls,javafx.deploy,javafx.swing,javafx.web)

所以如果你(javafx.graphics)在你的模块中添加模块它不起作用,

所以使用 :System.setProperty("javafx.preloader",path_class_loader)作为LauncherImplforjkd 9及以上的替代品

于 2018-05-30T10:11:21.067 回答
3

JDK 8:

LauncherImpl.launchApplication(Main.class, Preloader.class, arguments);

JDK 9:

System.setProperty("javafx.preloader", Preloader.class.getCanonicalName());
    Application.launch(Main.class, arguments);
于 2019-05-07T19:47:22.290 回答