0

Versions

openjdk version "11.0.11" 2021-04-20

OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10)

OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing)

IntelliJ IDEA 2021.1.2 (Ultimate Edition) Build #IU-211.7442.40, built on May 31, 2021

JavaFX SDK 11.0.2

Hey guys, first I want to say that this question was somewhat asked before here I followed all the steps that the correct answer gives, and end up with this error:

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:78)
    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:78)
    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:1071)
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:831)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6c5aff76) 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 @0x6c5aff76
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at KiloConverter.start(KiloConverter.java:12)
    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(AccessController.java:391)
    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.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)

I am somewhat of a beginner with Java, and I am doing this project for a college course. I can build the application, but cannot run it. Like I said, I followed all the steps the previous answer gave, to no avail.

Please if possible, don't tell me to "read up on the basics" like I saw other people answer before. I just am not sure why my code won't run for my final project.

Thank you so much in advance!

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • As you say you are just beginning Java, your life would be easier if you leave out modules from the picture for now. As a test, make a backup copy of `module-info.java` somewhere and remove it from your project. – tevemadar Jun 05 '21 at 00:17
  • Check [this answer](https://stackoverflow.com/a/54292408/2478398) and [this bug](https://github.com/javafxports/openjdk-jfx/issues/548). – BeUndead Jun 05 '21 at 00:18
  • @tevemadar The nature of the error indicates the OP does not have a `module-info` descriptor. Or at least is not launching their application as a module. – Slaw Jun 05 '21 at 02:31
  • It would be easier to advise if you included the sample project. – Andrey Jun 07 '21 at 12:17

1 Answers1

1

Since you mention you're a beginner in Java I first recommend reading this Q&A: What is a stack trace, and how can I use it to debug my application errors?. Then when you look back at your stack trace you'll see the fundamental error you're getting is:

Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6c5aff76) 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 @0x6c5aff76

This is a module-related error. You may want to read Understanding Java 9 Modules to familiarize yourself with modules.

In this case the error occurs because the javafx.fxml module is being found on the class-path while the javafx.graphics module is being found on the module-path. In other words, the javafx.fxml module is failing to be resolved as a named module but its code is still being found in the unnamed module. This breaks the qualified exports and qualified opens directives that javafx.graphics may give to javafx.fxml and the result is the IllegalAccessError.

You need to make sure the javafx.fxml module is actually resolved as a module. There are at least two ways to do this:

  1. Include javafx.fxml in your --add-modules JVM argument.

  2. Make your code modular by adding a module-info.java file:

    // or whatever you want to name your module
    module app {
      requires javafx.controls;
      requires javafx.fxml;
    
      // export your Application subclass's package to at least javafx.graphics
      exports com.example.app to javafx.graphics;
    
      // open your FXML controller package(s) to at least javafx.fxml (allows reflective access)
      opens com.example.app.controllers to javafx.fxml;
    }
    

    And launch your application as a module. For example, this is what it would look like on the command line:

    java --module-path <path> --module <module>/<main-class>
    
Slaw
  • 37,820
  • 8
  • 53
  • 80