Java 9 JavaFX Preloader

99封情书 提交于 2019-12-30 03:25:27

问题


In Java 8 I can launch a JavaFX application with a preloader using the following method:

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

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.

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?


回答1:


There is a comment on the answer to this question:

How to create splash screen as a Preloader in JavaFX standalone application?

system property javafx.preloader=classname seems to work too.

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.

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);



回答2:


from jdk 9, LauncherImpl not work jdk 10 - java.graphics module-info.java

all classes in package com.sun.javafx.application exported to special modules (java.base,javafx.controls,javafx.deploy,javafx.swing,javafx.web),

So if you add module (javafx.graphics) in your module its not work,

so use : System.setProperty("javafx.preloader",path_class_loader) as an alternative to LauncherImpl for jkd 9 and above




回答3:


JDK 8:

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

JDK 9:

System.setProperty("javafx.preloader", Preloader.class.getCanonicalName());
    Application.launch(Main.class, arguments);


来源:https://stackoverflow.com/questions/47533370/java-9-javafx-preloader

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