如何在应用程序构造函数中修复异常 [英] How to fix Exception in Application constructor
问题描述
我是Javafx的新手,我刚刚下载了JDK 12并遵循了教程,它对我有用,但不起作用,(我正在使用需要javafx.controls的模块)这是代码:在我的主班上:
I'm new in Javafx, I just downloaded JDK 12 and followed a tuturial, it has worked but not worked for me, (I'm using module to require javafx.controls) here is the code: in my main class :
我在SOFlow中尝试了很多解决方案,但没有结果,我尝试了:1)将公共关键字添加到我的班级2)删除主要方法仍然没有工作帮助吗?
I tried a lot of solutions in SOFlow but no result, I tried : 1) add public keyword to my class 2) removed the main method still not work help ?
package com.teachersdunet.hellojavafx;
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
}
}
这是执行它后的错误:
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:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.teachersdunet.hellojavafx.HelloApp
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:835)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:490)
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(AccessController.java:389)
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)
... 1 more
推荐答案
stacktrace中几乎提到了该解决方案.问题缩小到可以告诉您缺少的出口的位置:
The solution is almost mentioned in the stacktrace; the issue is narrowed down to the point where it tells you about the export that is missing:
...
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics
...
将以下行添加到 com.teachersdunet.hellojavafx
模块中:
Add the following line to the com.teachersdunet.hellojavafx
module:
module com.teachersdunet.hellojavafx {
...
exports com.teachersdunet.hellojavafx;
}
或者仅授予对单个模块的访问权限:
Or alternatively granting access to only a single module:
module com.teachersdunet.hellojavafx {
...
exports com.teachersdunet.hellojavafx to javafx.graphics;
}
这篇关于如何在应用程序构造函数中修复异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!