Search code examples
intellij-ideajavafxmedia-player

JavaFX MediaPlayer import


I have a issue, not being able to import MediaPlayer in my JavaFX project. I am using IntelliJ with a Maven build, JDK 17 .. I updated the Maven dependencies and I have the JavaFX plugin installed, and it is still not working.. Thanks in advance for any answer/suggestion

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.7.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.7</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.demo/com.example.demo.HelloApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and the module.info file

module com.example.demo {
    requires javafx.controls;
    requires javafx.fxml;


    opens com.example.demo to javafx.fxml;
    exports com.example.demo;
}

aswell as the VM arguments I added for the build/run

--module-path
"%Path to FX lib%"
--add-modules
javafx.controls,javafx.fxml,javafx.media

Solution

  • If you define a module-info.java file and you want to use the javafx.media module, you need to require that module in your module-info (as suggested by kleopatra in comments).

    module com.example.demo {
        requires javafx.controls;
        requires javafx.fxml;
        requires javafx.media;
        
        opens com.example.demo to javafx.fxml;
        exports com.example.demo;
    }
    

    The JavaFX modules are named in the javadoc.

    Oracle provides a brief introduction to the module system, which you should understand when you use it.

    As you are using Maven and JavaFX 17.0.0.1, you also need a dependency in Maven on that version of the javafx-media module.

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>17.0.0.1</version>
    </dependency>
    

    The java-media dependency version should be the same version as all other JavaFX dependencies. Using either the latest stable version (not an ea version) or the latest long-term service (LTS) release version is recommended.

    If using Idea, then:

    For a full discussion of this topic, see: