Java 9 JavaFX Preloader

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

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

我更喜欢从代码开始,如上所述,而不是使用部署配置,因为我不希望每次启动应用程序时都启动图形界面,但只有在计算出应用程序运行的某些代码之后GUI模式。

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

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

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

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

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

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

 public static void launchApplication(final Class appClass, final String[] args) { Class preloaderClass = savedPreloaderClass; if (preloaderClass == null) { String preloaderByProperty = AccessController.doPrivileged((PrivilegedAction) () -> System.getProperty("javafx.preloader")); if (preloaderByProperty != null) { try { preloaderClass = (Class) 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); 

从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)作为System.setProperty("javafx.preloader",path_class_loader) jkd 9及更高版本的LauncherImpl的替代品