Javafx 11: Illegalaccesserror When Creating Label

JavaFX 11: IllegalAccessError when creating Label

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.

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.

JavaFX Label Causes Exception

There's nothing wrong with your code! As you said you are starting now, I suppose you should have installed the latest version of Java: version 11.

What happens is that until version 10 of Java, if I'm not mistaken, JavaFX was included inside the JDK. But with the changes Oracle has made in its release policy, among other reasons, JavaFX is made available separately. Then you must install or enable JavaFX on your computer.

Here you can see how to install OpenFX and how to enable it in your project:
OpenFX

I hope this can help you!
Hugs!

java.lang.IllegalAccessException: module javafx.base cannot access class sample.model.Artists because module does not open sample.model to javafx.base

The module-info.java needs to be modified. When you defined the groupId as just "sample" (you can find it in the pom.xml) then this should work:

module-info.java

module sample {
requires javafx.controls;
requires javafx.fxml;

opens sample to javafx.fxml;
opens sample.model to javafx.fxml;

exports sample;
exports sample.model;
}

IllegalAccessError while using FXMLLoader

since Java 11 you need to add

--module-path C:[your path to]\javafx-sdk-13\lib --add-modules javafx.controls,javafx.fxml

to the run arguments of the project run configuration



Related Topics



Leave a reply



Submit