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
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.
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
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).