exceptionsAreBad
exceptionsAreBad

Reputation: 643

JavaFX module javafx.graphics

Upon fixing a requires issue with robot.awt I've now encountered another problem upon running my application. Application builds with no issues. Stack trace :

Exception in Application constructor
Exception in thread "main" 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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class reports.Main
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
    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.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class reports.Main (in module Reports) because module Reports does not export reports to module javafx.graphics
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
    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

Process finished with exit code 1

Upvotes: 12

Views: 17921

Answers (2)

iamfnizami
iamfnizami

Reputation: 193

  • As mentioned in above answer Application.launch uses reflection to create an instance of the Application class with the help of java reflection
  • But because of modules, reflection is inaccessible by default.
  • There are some cases where you have to enable reflection for your classes, In your case you will have to enable the reflection of your package named reports to the module javafx.graphics to let JavaFX work.
  • So, you need to modularize your application coz, JDK, JRE and JavaFX are modularized hence; create a new file module-info.java inside package src/main/java with below statements inside reports module block.

module reports {

requires javafx.controls;

requires javafx.media;

opens reports to javafx.graphics;

}

  • File module-info.java will define your application as a module named reports and your module requires some modules like (javafx.controls and javafx.media) in order to work.

Note: Module name reports is taken from your package name of Main class.

Why module?
Modules are a new way to organize your applications and libraries, it also enable strong encapsulation and implementation/reflection hiding.

Upvotes: 0

fabian
fabian

Reputation: 82471

The root cause for the issue is contained in this line:

Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class reports.Main (in module Reports) because module Reports does not export reports to module javafx.graphics

Application.launch uses reflection to create a instance of the application class using reflection. External classes like Application are only allowed to access your classes via reflections if the class recides in a package that is opened or exported to Application's module (javafx.graphics).

You need to add one of the following lines your Reports module declaration:

exports reports;
opens reports to javafx.graphics;

The last line should be preferred since it's the more restrictive one. If the unless the reports package also contains e.g. a main class/method, you should use the first line.

Upvotes: 26

Related Questions