gpt4 book ai didi

Java IllegalAccessError 无法运行应用程序

转载 作者:行者123 更新时间:2023-12-02 09:31:18 26 4
gpt4 key购买 nike

我正在为类(class)设置我的第一个程序,但我一直困惑为什么我的程序无法运行并尝试调试。我需要做什么来调试这个错误?

我尝试添加库,但这似乎不起作用。

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.awt.*;

public class Main extends Application {

@Override
public void start(Stage primaryStage) {
try{

Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
primaryStage.setTitle("My First JavaFX GUI");
Button button= new Button("Submit Answer");

StackPane layout= new StackPane();
layout.getChildren().add(button);

Scene scene1= new Scene(layout, 300, 250);
primaryStage.setScene(scene1);

primaryStage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

这是我的运行控制台给我的错误:

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 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
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: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
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.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x41b2844d) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x41b2844d
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at Main.start(Main.java:20)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
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
Exception running application Main

Process finished with exit code 1

我希望它能够运行,但它不会运行,而且我真的不知道该在哪里采取行动。谢谢参观 !如有任何帮助,我们将不胜感激:D

最佳答案

将模块添加到模块路径中。大多数项目只需要 javafx.controlsjavafx.fxml 但请检查您是否需要 other packages .

Run > Edit Configuration > VM Options >

--module-path /path/to/javafx-sdk-13/lib --add-modules javafx.controls,javafx.fxml

像这样: propper config

此外,请确保您的 SDK 和项目语言级别已正确设置:

File > Project Structure > Project

确保您的 OpenFX 库已添加(手动或使用 Gradle 之类的工具):

File > Project Structure > Libraries

最后一件事,我不认为您打算在代码中import java.awt.*;

关于Java IllegalAccessError 无法运行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57952124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com