Home >Java >javaTutorial >How to Resolve JavaFX Media Module Errors When Running a JavaFX 11 Application?

How to Resolve JavaFX Media Module Errors When Running a JavaFX 11 Application?

DDD
DDDOriginal
2024-12-20 17:49:12475browse

How to Resolve JavaFX Media Module Errors When Running a JavaFX 11 Application?

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:

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

2. Make Your Own Code Modular:

Create a module-info.java file in your project directory with the following content:

module my.application {
    requires javafx.controls;
    requires javafx.media;

    // Replace with your Application class' package
    exports com.example.myapp to javafx.graphics;
}

Then, launch your application with the --module argument to specify your custom module:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java 
--module my.application
--add-modules javafx.base,javafx.graphics,javafx.controls
-classpath ...

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn