Java 9 JavaFX Preloader [英] Java 9 JavaFX Preloader
问题描述
在Java 8中,我可以使用以下方法启动带有预加载器的JavaFX应用程序:
In Java 8 I can launch a JavaFX application with a preloader using the following method:
LauncherImpl.launchApplication(WindowMain.class, WindowMainPreloader.class,
new String[]{...});
我更喜欢从代码开始,如上所述,而不是使用部署配置,因为我不喜欢我希望每次启动应用程序时都启动图形界面,但只有在计算出应用程序应该以GUI模式运行的一些代码之后才会启动。
I prefer to start it from code, like above, instead of using a deploy configuration, because I don't want the graphical interface to start every time I start the application, but only after some code that has computed that the application should run in GUI mode.
我正在使用类com.sun.javafx.application.LauncherImpl,但显然在Java 9中,所有以com.sun开头的类都被删除了。那么,如何在Java 9中使用预加载器启动应用程序?
I was using the class "com.sun.javafx.application.LauncherImpl", but apparently in Java 9 all classes starting with "com.sun" are removed. So, how can I start the application with the preloader in Java 9?
推荐答案
对此问题的答案有评论:
There is a comment on the answer to this question:
系统属性
javafx.preloader = classname
似乎也有效。
我没有尝试过,但也许您可以尝试设置该属性,只需通过公共 Application.launch(appClass,args)
API和预载器可能会先启动。
I didn't try it, but perhaps you could try setting that property and just launching your main app via the public Application.launch(appClass, args)
API and perhaps the preloader will launch first.
查看 Application.launch
的代码,似乎这可行。以下是最终调用的代码,从Java 8源代码复制:
Looking into the code for Application.launch
, it seems that this would work. Here is the code which is eventually invoked, copied from the Java 8 source:
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);
}
所以你应该能够使用预载器启动应用程序:
So you should be able to launch an app with a preloader using:
System.setProperty("javafx.preloader", "my fully qualified preloader class name");
Application.launch(myMainClass, args);
这篇关于Java 9 JavaFX Preloader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!