在解决robot.awt的需求问题后,我现在在运行应用程序时遇到了另一个问题。应用程序构建没有问题。堆栈跟踪:
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
答案 0 :(得分:4)
此问题的根本原因包含在以下行中:
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
使用反射使用反射来创建应用程序类的实例。如果外部类位于打开或导出到Application
的模块(Application
)的包中,则仅允许外部类(例如javafx.graphics
)通过反射访问您的类。
您需要在Reports
模块声明中添加以下行之一:
exports reports;
opens reports to javafx.graphics;
最后一行应该是首选,因为它是限制性更强的一行。如果除非reports
软件包中还包含例如main
类/方法,您应该使用第一行。
答案 1 :(得分:0)
如果您使用的是Java 12,JavaFX 12和IntelliJ 您不再需要VM选项
答案 2 :(得分:0)
module reports {
requires javafx.controls;
requires javafx.media;
opens reports to javafx.graphics;
}
注意:模块名称 reports 取自 Main 的包名称 班级。
为什么是模块?
模块是一种组织应用程序和
库,它还支持强封装和
实现/反射隐藏。