我将我的旧JavaFX项目迁移到JavaFX。为此,我使用openjdk-17。启动此代码时,将捕获应用程序构造函数中的异常。也许我用了不对的词?但是为什么编译器在构建我的项目时会说好呢?
我的堆栈追踪
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:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class classes.Main
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class classes.Main (in module com.example.javacoursework) because module com.example.javacoursework does not export classes to module javafx.graphics
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
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:184)
... 1 more我的代码
package classes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
public class Main extends Application //Error occured in constructor!?
{
@Override
public void start(Stage primaryStage) throws IOException
{
Button button = new Button("Hello");
Scene scene = new Scene(button);
primaryStage.setScene(scene);
primaryStage.show();
// Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("../../resources/123.fxml")));
// primaryStage.setTitle("Vote app");
// primaryStage.setScene(new Scene(root));
// primaryStage.setResizable(false);
// primaryStage.show();
}
public static void main(String[] args)
{
//launch(args);
System.out.println("Hello World!");
}
}如果需要更多的信息,我会添加到这个帖子。
发布于 2022-03-20 21:43:33
JavaFX框架使用反射实例化您的Application实现类。如果所述模块提供了必要的“权限”,则代码只能反射地访问单独模块中的代码。如果没有授予这些“权限”,则将引发IllegalAccessException。
这里是您的根本例外:
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class classes.Main (in module com.example.javacoursework) because module com.example.javacoursework does not export classes to module javafx.graphics解决办法很简单。只需向模块信息描述符(module-info.java)添加以下指令:
exports classes to javafx.graphics;需要导出的包以及需要导出的模块可以通过读取异常消息来确定。注意,您只需要使用exports而不是opens,因为JavaFX应用程序类及其构造函数必须是public。
它可能有助于阅读:What is a stack trace, and how can I use it to debug my application errors?
发布于 2022-03-20 11:29:11
问题不在您的代码中。这是你的项目/启动设置。到这里来看看如何正确配置Maven。https://openjfx.io/openjfx-docs/
https://stackoverflow.com/questions/71546089
复制相似问题