0

I'm trying to add javafx.media module. I'm using IDEA with Maven build tool. I did some online searches and added required module to modules-info.java and javafx-media dependency to pom.xml. The prebuild found no error but when I tried to build the project it said: "java: package javafx.scene.media does not exist". I tried to Google it again but found nothing except what I've done. So please help me. I'm new to Maven and Java 9 Modules.

Here is my module-info.java file content

module com.example.minesweeper {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.media;

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

Here is my pom.xml file content

<?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>Minesweeper</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Minesweeper</name>

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

  <dependencies>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>20-ea+4</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-fxml</artifactId>
      <version>20-ea+4</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>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-media</artifactId>
      <version>17</version>
    </dependency>
  </dependencies>

  <build>
        <plugins>
            <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
          <source>20</source>
          <target>20</target>
        </configuration>
            </plugin>
      <plugin>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>0.0.8</version>
        <executions>
          <execution>
            <!-- Default configuration for running with: mvn clean javafx:run -->
            <id>default-cli</id>
            <configuration>
              <mainClass>com.example.minesweeper/com.example.minesweeper.MinesweeperApplication</mainClass>
              <launcher>app</launcher>
              <jlinkZipName>app</jlinkZipName>
              <jlinkImageName>app</jlinkImageName>
              <noManPages>true</noManPages>
              <stripDebug>true</stripDebug>
              <noHeaderFiles>true</noHeaderFiles>
            </configuration>
          </execution>
        </executions>
      </plugin>
          </plugins>
  </build>
</project>

Sometimes there are other packages not found too but I don't know how do they not exist and I just revert the project to fix them. I hope I can find a general solution to these missing packages.

  • 1
    Is there a good reason why you are using a `20-ea+4` version instead of `20.0.1`? https://central.sonatype.com/artifact/org.openjfx/javafx-controls/21-ea%2B23/versions and why is `javafx-media` using version 17 instead of `20.0.1`? – khmarbaise Jun 26 '23 at 10:42
  • 1
    Thank you so much for your comment. I tried javafx-media 20.0.1 and it worked. I don't know why the 17 version contains the package javafx.scene.media but it missing some classes and it couldn't be found. So it's all about my ignorance of Maven. – Giao Le Xuan Jun 26 '23 at 11:09
  • No problem glad to help. Post your knowledge as answer that might help others in the future... – khmarbaise Jun 26 '23 at 14:42
  • I'm not really understand what happened so I don't want to post an answer. The versions I used was defaulted by IDEA and I didn't understand much about how Maven works. So the temporary solution is just switch to a newer versions which can be found on https://mvnrepository.com/ and modifying pom.xml. For general problem of missing some random packages, I haven't known the solution yet. – Giao Le Xuan Jun 26 '23 at 15:29
  • IDEA and I didn't understand much about how Maven works ? Really.. it does understand a lot... The versions should be looked up in central repository https://central.sonatype.com/ – khmarbaise Jun 26 '23 at 15:33
  • Note that there haven't been any packages added to or removed from the `javafx.media` module between versions 17 and 20. So, while the mismatched versions seemed to be the problem, I don't know why that lead to a "package does not exist" error. – Slaw Jun 27 '23 at 02:30

0 Answers0