Issue
I´m using javaFX (13.0.2) to make a project and I need to use a preloader, but the LauncherImpl.launchApplication()
is not successful.
code:
package classes;
import com.sun.javafx.application.LauncherImpl;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
System.out.println("start");
}
@Override
public void init() throws Exception {
System.out.println("init");
}
public static void main(String[] args) {
LauncherImpl.launchApplication(Main.class, UIloadSystem.class, args); // - Not works
LauncherImpl.launchApplication(Main.class, args); // - Not works
//launch(Main.class, args); - Works
}
}
Yes, is a very simple code
Error:
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.IllegalAccessError: class classes.Main (in unnamed module @0x1936f0f5) cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module @0x1936f0f5
at classes.Main.main(Main.java:30)
... 11 more
Exception running application classes.Main
I researched on top of this part class classes.Main (in unnamed module @0x1936f0f5) cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module @0x1936f0f5
, but
I didn't find this event anywhere.
If are imporant, I'm using ItelliJ IDEA and the VM options are --module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
Sorry for my english, if are some grammatical errors :|
Solution
com.sun.javafx.application.LauncherImpl
is private API and not intended for normal developers. Note the fact it starts com.sun
. All the public JavaFX API is contained in packages javafx.*
. From Java 9 onwards private API is no longer accessible, however some older tutorials still exist.
To use a preloader the following approach should be taken after Java 9.
public static void main(String[] args){
System.setProperty("javafx.preloader", UIloadSystem.class.getCanonicalName());
launch(args);
}
Answered By - Adam
Answer Checked By - Gilberto Lyons (JavaFixing Admin)