0

Whenever I use JavaFX's FXMLLoader.load(URL) I end up with InvocationTargetException like this:

Exception in Application start method
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:566)
    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:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x37b175c4) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x37b175c4
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at net.chatbrain.desktop.misc.StageManager.changeScene(StageManager.java:90)
    at net.chatbrain.desktop.misc.StageManager.startup(StageManager.java:81)
    at net.chatbrain.desktop.models.MainStage.start(MainStage.java:18)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application net.chatbrain.desktop.models.MainStage

However, I was able to fix the problem by adding a bunch of --add-export VM-Flags, e.g. --add-exports=javafx.graphics/com.sun.javafx.reflect=ALL-UNNAMED

I have added like 20 of these, each one of them apparently solves problem, and each time I add one of these, another arose until I came to roughly 10. After the GUI finally worked, but as I proceeded to develop, rerunning spawned new InvocationTargetExceptions. This has since happened for every change I made. Adding this large amount did solve the problem,but it's very inconvenient and inelegant to me. Additionaly, I have to apply this everytime I'm working with JavaFX. So my question: Is there any better alternative to this that fixes this permanentely? Thanks in advance, John

  • 1
    Yes. `--add-export` is not necessary if you just add the required modules like `--add-modules javafx.controls,javafx.fxml`. Very similar question: [IntelliJ IDEA - Error: JavaFX runtime components are missing, and are required to run this application](https://stackoverflow.com/questions/52906773/intellij-idea-error-javafx-runtime-components-are-missing-and-are-required-t/) – José Pereda Apr 21 '19 at 12:26
  • @JoséPereda Adding that as vm option gave me a `java.lang.module.FindException: Module javafx.controls not found` error. However, I did look into your earlier post and im missing the module path, but that is because im using maven. Do i just put the path to the jar in the maven repository? – AmazingJonnie Apr 21 '19 at 19:19
  • If you are using Maven... what plugin do you use to launch the project? It looks like you are using your IDE, though, which doesn't use Maven. – José Pereda Apr 21 '19 at 19:42
  • @JoséPereda yes, I do compile and launch the projects with IntelliJ. I only build them with maven once im done – AmazingJonnie Apr 23 '19 at 15:40
  • IntelliJ doesn't use your Maven pom, it tries to launch the app on its own via the `java` command, and that's why you need to set `--module-path` and `--add-modules` as VM options. See https://openjfx.io/openjfx-docs/#IDE-Intellij (both IDE and Maven). Also check this [question](https://stackoverflow.com/questions/52741129/different-behaviour-between-maven-eclipse-to-launch-a-javafx-11-app). – José Pereda Apr 23 '19 at 15:57

0 Answers0