4

I'm using Intellij with javafx 12, as a separate library. I managed to make it work. I made a game, and the graphics work great. Now I wanted to create a menu, with some buttons. It throws an exception when I try to add a button to the root of the scene, With the following exception:

Exception in thread "JavaFX Application Thread" java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x77b376ef) 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 @0x77b376ef

I googled it and found some solutions on stack overflow, which don't work for me. My command line arguments are the following (based on those solutions):

--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" \
--add-modules=javafx.swing,javafx.graphics,javafx.fxml,javafx.media,javafx.web,javafx.scene,javafx.controls \
-p "C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.base.jar" : \
   "C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.graphics.jar"  \
--add-exports javafx.graphics=ALL-UNNAMED \
--add-exports javafx.controls/com.sun.javafx.charts=ALL-UNNAMED \
--add-exports javafx.graphics/com.sun.javafx.iio=ALL-UNNAMED \
--add-exports  javafx.graphics/com.sun.javafx.iio.common=ALL-UNNAMED \
--add-exports  javafx.graphics/com.sun.javafx.css=ALL-UNNAMED \
--add-exports  javafx.base/com.sun.javafx.runtime=ALL-UNNAMED \
--add-exports javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED \
--add-exports javafx.graphics/com.sun.javafx.scene.layout=ALL-UNNAMED

Thanks!

Edit 1: Minimal reproducible example

  1. download the latest intellij
  2. create a new javafx template project
  3. add javafx libraries
  4. add the following line to the command line arguments in run configurations

    --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" \ --add-modules javafx.fxml,javafx.controls

Note: Please check that the path to the libraries is right.

  1. Change the start method to the following:

    @Override public void start(Stage primaryStage) throws Exception{ Button button = new Button(); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(new Group(), 300, 275)); primaryStage.show(); }

Important note: Please make sure that the button is from javafx, not awt library! This will work if you import button from awt by mistake.

The button is never added to the root (nor the scene), and if you comment the line which creates the button, everything works!

Edit 2:

It looks like I'm running the configuration I have created, since the command line that intellij creates (when I click on ...) is the follwing:

"C:\Program Files\Java\jdk-12.0.2\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\lib\idea_rt.jar=50712:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\dusan\Documents\untitled\out\production\untitled;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx-swt.jar" -p "C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.graphics.jar" sample.Main --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" \ --add-modules javafx.fxml,javafx.controls
Dusan Milosevic
  • 460
  • 2
  • 4
  • 18
  • 1
    You are running into the issue in this [question](https://stackoverflow.com/questions/54291958/javafx-11-illegalaccesserror-when-creating-label/), however you are adding the VM options. The only explanation is that you are not running the configuration you have created. So make sure you do run it. Also check the command line that IntelliJ creates when you hit run (you can click the `...` in the run window) – José Pereda Sep 22 '19 at 17:07
  • I edited the answer. It looks like I'm running the configuration, based on the command line that intellij creates. Also, if I add, for example circle to the scene (and comment the button line), it works as expected. So I suspect the problem is with javafx.controls. Could you please verify if this minimal example works on your machine? Thanks! – Dusan Milosevic Sep 22 '19 at 17:28
  • 1
    In your command line of Edit 2, the VM arguments are added _after_ the main class, so they are interpreted as Runtime arguments. Make sure you add them as _VM arguments_. – José Pereda Sep 22 '19 at 17:53
  • Oh, thank you so much, this was the problem. I don't know why I added these commands as command line arguments, instead of VM options... – Dusan Milosevic Sep 22 '19 at 18:30

1 Answers1

2

The answer is in your command line:

--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" \
...
-p "C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.base.jar" : \
   "C:\Program Files\Java\javafx-sdk-11.0.2\lib\javafx.graphics.jar"  \

Note that the option -p is exactly the same as --module-path (see application module path in JEP 261).

This means that you are adding it twice, but only the last prevails. So only javafx-base and javafx-graphics are added to the module path, therefore javafx-controls is not. This explains the exception (related to com.sun.javafx.scene.control.ControlHelper), and the fact that you added unnecessarily all those --add-exports.

Probably you won't be using Swing, Media or Web, so this command line will be more than enough:

--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" \
--add-modules javafx.fxml,javafx.controls

These VM options can be set in your IDE.

All of these is documented properly here: https://openjfx.io/openjfx-docs/ (also check the documentation for your specific IDE).

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Unfortunately, I'm getting the same exception again. I think my first command line arguments were those, and then i started adding others based on stack overflow answers. – Dusan Milosevic Sep 22 '19 at 16:20
  • 1
    Adding solutions from others won't work if you don't know how these will apply. I strongly suggest you go through the documentation I linked above. If you use an IDE, make sure you set correctly the VM options. You can also edit and update your question with more details that help reproducing your issue. – José Pereda Sep 22 '19 at 16:22
  • I edited my answer and added the minimal reproducible example. Thanks! – Dusan Milosevic Sep 22 '19 at 17:02