尝试在Java SDK 15上,使用来自JavaFX 15的模块,在IntelliJ中运行JavaFX。

huangapple go评论73阅读模式
英文:

Trying to run javaFX on java sdk 15 with the module from javaFX 15 in IntelliJ

问题

这是错误,但仅在使用Button类时才会发生。

启动应用程序方法中的异常
java.lang.reflect.InvocationTargetException
    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在 java.base/java.lang.reflect.Method.invoke(Method.java:564)
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在 java.base/java.lang.reflect.Method.invoke(Method.java:564)
    在 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    在 java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper(在无名模块 @0x5b3ec5dc 中)无法访问类 com.sun.javafx.scene.layout.RegionHelper(在模块 javafx.graphics 中) ,因为模块 javafx.graphics 未将 com.sun.javafx.scene.layout 导出到无名模块 @0x5b3ec5dc
    在 java.base/java.lang.ClassLoader.defineClass1(Native Method)
    在 java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    在 java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    在 java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
    在 java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
    在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
    在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
    在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    在 javafx.scene.control.Control.<clinit>(Control.java:86)
    在 Main.start(Main.java:17)
    在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    在 java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    在 javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
运行应用程序时出现异常 Main

这是代码

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Button button = new Button();
        button.setText("Click Me");
        stage.setTitle("First JavaFX GUI");

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

        Scene scene = new Scene(layout, 300, 250);
        stage.setScene(scene);
        stage.show();
    }
}

当我移除Button时,它可以正常运行并显示窗口,但使用Button时会抛出该错误。我该如何解决这个问题?我目前在使用IntelliJ的Mac上,使用JDK 15和JavaFX 15。

英文:

This is the error, however it only occurs when using the Button class.

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:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	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:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
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:832)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x5b3ec5dc) 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 @0x5b3ec5dc
	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:151)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at javafx.scene.control.Control.<clinit>(Control.java:86)
	at Main.start(Main.java:17)
	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(AccessController.java:391)
	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)
Exception running application Main

This is the code

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Button button = new Button();
        button.setText("Click Me");
        stage.setTitle("First JavaFX GUI");

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

        Scene scene = new Scene(layout, 300, 250);
        stage.setScene(scene);
        stage.show();
    }
}

When I remove the Button, it runs fine and shows the window, however when using Button it throws that error. How can I solve this. I am currently in a Mac, using IntelliJ. JDK 15 with JavaFX 15.

答案1

得分: 0

如果您正在使用IntelliJ,您应该在运行配置的VM选项下添加以下内容:
--module-path PathToJavaFX --add-modules javafx.controls,javafx.fxml。

PathToJavaFx 应该是 /lib 文件夹。

英文:

If you are using IntelliJ you should add this to the VM options under the Run Configurations.
--module-path PathToJavaFX --add-modules javafx.controls,javafx.fxml.

the PathToJavaFx should be the /lib folder.

答案2

得分: 0

我在使用JavaFX和模块方面遇到了很大的挑战。到目前为止,我已经解决了如何结合使用Maven和SQL,但是如果我添加一个module-info文件,就会失败。

基本上,我遇到的问题(产生了与您相同的结果)通过以下方式解决:要么移除module-info和移除Maven,但是将OpenJFX作为项目的库,要么移除库和module-info,但是通过Maven获取OpenJFX。

问题在于重复使用JavaFX的位置/引用,以及module-info文件混乱了项目结构。

我想分享一下对我有用的方法。祝你好运!

英文:

I've had a very challenging time working with JavaFX and modules. So far I've worked out how to incorporate Maven and SQL, but if I add in a module-info file, it fails.

Basically, the issue that I ran into (which produced the same result that you're having) was solved by me either: removing module-info and removing Maven, but having OpenJFX as a library for the project, or removing the library & module-info, but acquiring OpenJFX via Maven.

The issue was doubling up on JavaFX locations/references and the module-info file mucking up the works.

Thought it might be useful to share what's been working for me. Good luck!

huangapple
  • 本文由 发表于 2020年10月14日 12:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64346471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定