Java IllegalAccessError无法运行应用程序 [英] Java IllegalAccessError can't run Application

查看:794
本文介绍了Java IllegalAccessError无法运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个类设置我的第一个程序,但我坚持为什么我的程序在尝试调试时将无法运行.我需要怎么做才能调试此错误?

I'm setting up my first program for a class and I'm stuck on why my program won't run trying to debug. What do I need to do to debug this error?

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

I've tried to add libraries but this didn't seem to work.

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);
    }
}

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

This is the error my Run Console is giving me:

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

I expect this to run, but it won't and I'm really stuck on where to act. Thanks for taking a look ! Any help is appreciated :D

推荐答案

将模块添加到模块路径.大多数项目只需要javafx.controlsjavafx.fxml,但请检查是否需要其他软件包.

Add the modules to your modulepath. Most projects just need javafx.controls and javafx.fxml but check to see if you need other packages.

运行>编辑配置> VM选项>

Run > Edit Configuration > VM Options >

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

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

像这样:

另外,请确保在以下位置正确设置了SDK和项目语言级别:

Additionally make sure that your SDK and project language level is properly setup in:

文件>项目结构>项目

File > Project Structure > Project

确保在以下位置添加了OpenFX库(手动添加或使用Gradle之类的东西):

Make sure that your OpenFX library is added (either manually or with something like Gradle) in:

文件>项目结构>图书馆

File > Project Structure > Libraries

最后一件事,我认为您不是要在代码中使用import java.awt.*;.

And one last thing, I don't think you meant to import java.awt.*; in your code.

这篇关于Java IllegalAccessError无法运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆