当我将代码复制并粘贴到几周前创建的另一个javafx程序中时,我的代码可以运行,但是由于某种原因,每当我创建新的javafx程序时,我都无法运行它们并获得这些错误:
Exception in Application start method
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: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0xb69df6e) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0xb69df6e
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javafx.scene.control.Control.<clinit>(Control.java:86)
at Homework4.Homework4.start(Homework4.java:24)
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 Homework4.Homework4
我认为我的JDK11 / JavaFX11出了点问题,不允许我创建更多的JavaFX项目,因为有些原因使每个新项目都有此编译错误。我认为该错误是由于java.lang.IllegalAccessError引起的,但我不知道如何解决它。
这是我的参考代码:
package Homework4;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Homework4 extends Application {
private TextField firstNum;
private TextField secondNum;
private Button plus;
private Button equals;
private TextField result;
@Override
public void start(Stage stage) {
firstNum = new TextField();
firstNum.setMaxWidth(100);
firstNum.setOnAction(event -> handleEquals(event));
secondNum = new TextField();
secondNum.setMaxWidth(100);
secondNum.setOnAction(event -> handleEquals(event));
plus = new Button("+");
plus.setOnAction(event -> handleEquals(event));
equals = new Button("=");
equals.setOnAction(event -> handleEquals(event));
result = new TextField();
result.setEditable(false);
HBox innerPane = new HBox();
innerPane.setSpacing(10);
innerPane.setPadding(new Insets(10, 10, 10, 10));
innerPane.getChildren().addAll(firstNum, plus, secondNum, equals);
VBox pane = new VBox();
pane.setPadding(new Insets(10, 10, 10, 10));
pane.getChildren().addAll(innerPane, result);
stage.setScene(new Scene(pane));
stage.setTitle("Welcome to Calculator 3000!");
stage.show();
}
private void handleEquals(ActionEvent event){
if(!(firstNum.getText().equals("") || secondNum.getText().equals(""))){
result.setText(firstNum.getText() + secondNum.getText());
}
}
public static void main(String[] args) {
launch(args);
}
}
我当前正在使用最新更新的IntelliJ Idea进行编码。
侧面说明,我目前是大学新生,刚开始创建javafx项目,因此欢迎您提供帮助/建议!
我试图在此线程中寻求帮助:InvocationTargetException when running a javafx program
很遗憾,我没有编码知识,无法理解人们在该主题中发布的建议。
答案 0 :(得分:0)
事实证明我没有正确设置IntelliJ,这导致了我的InvocationTargetException。假设您所有的环境变量都是正确的,请遵循以下instructions来解决此异常。