JavaFX 11: IllegalAccessError when creating Label

前端 未结 3 1288
南方客
南方客 2020-11-22 05:41

This question is likely about the same issue as this one, but it appears the asker of that one hasn\'t added enough information to receive a helpful response.

I am t

相关标签:
3条回答
  • 2020-11-22 06:19

    I would use gradle and the javafx plugin for that.

    apply plugin: "org.openjfx.javafxplugin"
    
    javafx {
        version = "11"
        modules = ["javafx.base", "javafx.controls", "javafx.graphics"
    }
    
    0 讨论(0)
  • 2020-11-22 06:24

    You are already giving an explanation of your issue:

    I have not modified any VM options.

    Since JavaFX 11 is not part of the JDK anymore, you have to use the JavaFX SDK (as you are doing) from here, or alternatively use Maven/Gradle to retrieve the JavaFX modules from Maven Central.

    Then you need to add the SDK as a library, so IntelliJ can find the JavaFX classes.

    But once you have done that, and given that the JavaFX jars are modules, you still need to do two things:

    • Make the JavaFX modules available to your module-path
    • Define which modules do you add to the project

    Based on your IntelliJ output, it is adding by default javafx.graphics and javafx.base:

    --add-modules javafx.base,javafx.graphics
    
    -p /usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.base.jar
    :/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.graphics.jar
    

    (note that -p is the same as --module-path)

    This explains why your project runs in the first case, when you don't have a control added to the scene, just the StackPane, that belongs to the javafx.graphics module, but fails with the posted exception when you add the Label, that belongs to the javafx.controls module.

    This has been said a number of times: you need to set the required VM options for your project.

    Start by reading the documentation at https://openjfx.io/openjfx-docs/, including the IntelliJ doc, section Non-modular projects for your IDE. And read the part 4. Add VM options.

    So click on Run -> Edit Configurations, and add:

    -p /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls
    

    Apply, and run, the issue will be solved.

    0 讨论(0)
  • 2020-11-22 06:46

    check your JAVA_HOME, JAVA_HOME'version must be jdk9+ or jdk11+

    0 讨论(0)
提交回复
热议问题