如何修复应用程序构造函数中的异常

Ibr*_*ane 2 java javafx

我是 Javafx 新手,我刚刚下载了 JDK 12 并遵循了教程,它已经工作但对我不起作用,(我使用模块来要求 javafx.controls)这里是代码:在我的主类中:

我在 SOFlow 中尝试了很多解决方案,但没有结果,我尝试过:1)将 public 关键字添加到我的类中 2)删除了 main 方法仍然不起作用帮助?

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 {



    }


}
Run Code Online (Sandbox Code Playgroud)

这是执行后的错误:

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
Run Code Online (Sandbox Code Playgroud)

fab*_*ian 5

解决方案几乎在堆栈跟踪中提到;该问题已缩小到它告诉您缺少导出的程度:

...
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
...
Run Code Online (Sandbox Code Playgroud)

将以下行添加到com.teachersdunet.hellojavafx模块中:

module com.teachersdunet.hellojavafx {
    ...
    exports com.teachersdunet.hellojavafx;
}
Run Code Online (Sandbox Code Playgroud)

或者仅授予对单个模块的访问权限:

module com.teachersdunet.hellojavafx {
    ...
    exports com.teachersdunet.hellojavafx to javafx.graphics;
}
Run Code Online (Sandbox Code Playgroud)