JavaFX二叉搜索树可视化应用启动报错求助
JavaFX二叉搜索树可视化应用启动报错:IllegalAccessException
问题描述
使用IntelliJ IDEA开发基于JavaFX的二叉搜索树(BST)可视化应用,源码检查无问题,但启动时报错,核心原因是模块com.special.effect.javafxprojects未将com.special.effect.javafxprojects.BinarySearchTreeApplication包导出至javafx.graphics模块,导致IllegalAccessException。
完整报错信息
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Users\FAbdu\IntelliJ IDEA 2023.1\lib\idea_rt.jar=60595:C:\Users\FAbdu\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-controls\19\javafx-controls-19.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-graphics\19\javafx-graphics-19.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-base\19\javafx-base-19.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-fxml\19\javafx-fxml-19.jar;C:\Users\FAbdu\.m2\repository\org\jetbrains\annotations\17.0.0\annotations-17.0.0.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-media\21-ea+5\javafx-media-21-ea+5.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-media\18-ea+1\javafx-media-18-ea+1.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-graphics\18-ea+1\javafx-graphics-18-ea+1.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-base\18-ea+1\javafx-base-18-ea+1.jar -p C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-base\19\javafx-base-19-win.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-base\18-ea+1\javafx-base-18-ea+1-win.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-media\21-ea+5\javafx-media-21-ea+5-win.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-media\18-ea+1\javafx-media-18-ea+1-win.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-graphics\19\javafx-graphics-19-win.jar;C:\Users\FAbdu\.m2\repository\org\jfxcore\javafx-graphics\18-ea+1\javafx-graphics-18-ea+1-win.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-fxml\19\javafx-fxml-19-win.jar;C:\Users\FAbdu\.m2\repository\org\openjfx\javafx-controls\19\javafx-controls-19-win.jar;C:\Users\FAbdu\IdeaProjects\JavaFXprojects\target\classes -m com.special.effect.javafxprojects/com.special.effect.javafxprojects.BinarySearchTreeApplication.BSInterface Exception in Application constructor Exception in thread "main" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.special.effect.javafxprojects.BinarySearchTreeApplication.BSInterface at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891) at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:1589) Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.special.effect.javafxprojects.BinarySearchTreeApplication.BSInterface (in module com.special.effect.javafxprojects) because module com.special.effect.javafxprojects does not export com.special.effect.javafxprojects.BinarySearchTreeApplication to module javafx.graphics at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:420) at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:709) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:493) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:484) at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803) at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484) at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456) at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) ... 1 more Process finished with exit code 1
应用UI代码
public class BSInterface extends Application { @Override public void start(Stage stage) throws Exception { BST<Integer> tree = new BST<>(); BorderPane pane = new BorderPane(); BSTbackend view = new BSTbackend(tree); pane.setCenter(view); TextField tfKey = new TextField(); tfKey.setPrefColumnCount(3); tfKey.setAlignment(Pos.BASELINE_RIGHT); Button btnInsert = new Button("INSERT"); Button btnDelete = new Button("DELETE"); HBox hBox = new HBox(5); hBox.getChildren().addAll(new Label("Enter a key: "), tfKey, btnInsert, btnDelete); hBox.setAlignment(Pos.CENTER); pane.setBottom(hBox); btnInsert.setOnAction(e->{ int key = Integer.parseInt(tfKey.getText()); if(!tree.search(key)){ view.displayTree(); view.setStatus(key + " is not in the tree"); }else{ tree.delete(key); view.displayTree(); view.setStatus(key + " is deleted from the tree"); } }); btnDelete.setOnAction(e->{ int key = Integer.parseInt(tfKey.getText()); if(!tree.search(key)){ view.displayTree(); view.setStatus(key + " is not in the tree"); }else{ tree.delete(key); view.displayTree(); view.setStatus(key + " is deleted from the tree"); } }); Scene scene = new Scene(pane, 450, 250); stage.setTitle("BST Animation"); stage.setScene(scene); stage.show(); } }
解决方案
该错误由Java模块系统的访问控制机制导致:JavaFX的javafx.graphics模块需要通过反射访问你的BSInterface类(继承自Application),但你的模块未开放对应包的访问权限。
方法1:修改module-info.java
找到项目中的module-info.java文件,添加导出语句,将包含BSInterface的包明确导出给javafx.graphics模块:
module com.special.effect.javafxprojects { requires javafx.controls; requires javafx.graphics; requires javafx.fxml; // 导出目标包给javafx.graphics模块 exports com.special.effect.javafxprojects.BinarySearchTreeApplication to javafx.graphics; }
方法2:确保类的访问权限
确认BSInterface类使用public修饰符。JavaFX启动器需要访问类的公共构造方法,若类使用默认包访问权限,会触发访问异常。
方法3:检查模块路径配置
在IntelliJ IDEA的运行配置中,确认模块路径(Module path)正确包含你的项目模块,且JavaFX依赖已添加到模块路径中,而非仅类路径。
内容的提问来源于stack exchange,提问作者Farkhod Abdukodirov
相关产品推荐
相关产品推荐

