
Module error when running JavaFx media application
Problem:
When running a JavaFX 11 application with the command provided in the question, an error is encountered related to illegal access between classes in the javafx.media and javafx.graphics modules.
Solution:
To resolve the error and run the application successfully, ensure that the javafx.media module is resolved as a named module during application launch. This can be accomplished through one of the following methods:
1. Include the Module in the --add-modules Argument:
Modify the command to include javafx.media as an additional module in the --add-modules argument:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /usr/lib/jvm/java-11-openjdk-amd64/bin/java
-p ~/.m2/repository/org/openjfx/javafx-swing/11/javafx-swing-11.jar:
~/.m2/repository/org/openjfx/javafx-swing/11/javafx-swing-11-linux.jar:
~/.m2/repository/org/openjfx/javafx-graphics/11/javafx-graphics-11.jar:
~/.m2/repository/org/openjfx/javafx-graphics/11/javafx-graphics-11-linux.jar:
~/.m2/repository/org/openjfx/javafx-base/11/javafx-base-11.jar:
~/.m2/repository/org/openjfx/javafx-base/11/javafx-base-11-linux.jar:
~/.m2/repository/org/openjfx/javafx-controls/11/javafx-controls-11.jar:
~/.m2/repository/org/openjfx/javafx-controls/11/javafx-controls-11-linux.jar:
~/.m2/repository/org/openjfx/javafx-media/11/javafx-media-11.jar:
~/.m2/repository/org/openjfx/javafx-media/11/javafx-media-11-linux.jar
--add-modules javafx.controls,**javafx.media**
-classpath ...
|
Copy after login
2. Make Your Own Code Modular:
Create a module-info.java file in your project directory with the following content:
1 2 3 4 5 6 7 | module my.application {
requires javafx.controls;
requires javafx.media;
exports com.example.myapp to javafx.graphics;
}
|
Copy after login
Then, launch your application with the --module argument to specify your custom module:
1 2 3 4 | /usr/lib/jvm/java-11-openjdk-amd64/bin/java
--module my.application
--add-modules javafx.base,javafx.graphics,javafx.controls
-classpath ...
|
Copy after login
3. Use a JDK that Includes JavaFX:
Download a JDK that includes JavaFX, such as BellSoft Liberica or Azul Zulu, and use that to run your application.
Additional Notes:
- It is recommended to avoid placing both the JavaFX JAR files and JavaFX modules on the classpath simultaneously. This can lead to the error described in the question.
- If you encounter compilation errors related to missing symbols after changing to modular code, ensure that you have the latest Java Development Kit (JDK) installed.
- Refer to the JavaFX documentation for further information on modularity and the Java Platform Module System: https://openjfx.io/javadoc/11/javadoc/index.html
The above is the detailed content of How to Resolve JavaFX Media Module Errors When Running a JavaFX 11 Application?. For more information, please follow other related articles on the PHP Chinese website!