5

I am trying to build a virtual piano app where i need the MediaPlayer class to play the notes, my project is a modular maven project with fxml, javafx 11.0.2 and java 14.

The problem that i can not import the MediaPlayer class, i tried to add requires javafx.media; to my module-info.java but it doesn't recognize it anyways. Here is my module-info.java

module org.project {
    requires javafx.controls;
    requires javafx.fxml;

    opens org.project to javafx.fxml;
    exports org.project;
}

enter image description here

Also tried to download the library jar but it is empty.

Also tried to add a maven dependency in the pom.xml file as following:

<!-- https://mvnrepository.com/artifact/org.openjfx/javafx.media -->
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx.media</artifactId>
    <version>11.0.0-ea1</version>
    <type>pom</type>
</dependency>

My pom.xml dependencies:

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

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx.media -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx.media</artifactId>
        <version>15</version>
        <type>pom</type>
    </dependency>
</dependencies>

and it shows that the repository is not found.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Malek Anas
  • 91
  • 1
  • 7

2 Answers2

6

Note: This answer applies to other JavaFX modules such as fxml and web, as well.


javafx.media is a module, you need to require it in your module-info.java like you do for javafx.controls and javafx.fxml.

module org.project {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.media;

    opens org.project to javafx.fxml;
    exports org.project;
}

There may be other issues, but that is a likely one.


I don't recommend using the 11.0.0-ea1 release, there are later more stable versions. Latest stable release of JavaFX is 15 https://mvnrepository.com/artifact/org.openjfx/javafx-media/15, so I would advise you use that for all of your JavaFX dependencies.

<!-- https://mvnrepository.com/artifact/org.openjfx/javafx.media -->
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-media</artifactId>
    <version>15</version>
    <type>pom</type>
</dependency>

For later readers referring to this post, please use the most up to date non-early access release you can find using mvnrepository or a similar search tool.

tried to download the library jar but it is empty

Don't do this for JavaFX dependencies. JavaFX needs to include both native libraries and Java libraries. Just downloading Jar files may not work for you as you may be missing required stuff. Plus there may be transitive imports for libraries required which, again, just downloading a single jar won't pick up. You are already using a dependency hierarchy build tool in Maven, just configure and use it correctly and let it do its job.

Additionally, I think the javafx.media artifact was incorrectly named, configured and uploaded to maven central, which is why it only existed there as a 11.0.0-ea1 version. It should have been named javafx-media, which it is on subsequent uploads, so use the correct name for the artifact and then the most recent versions are there which include all the correct artifact data in the maven central repository.

My pom.xml dependencies

Your pom.xml dependencies mix JavaFX versions for different JavaFX dependencies. Don't do this, the different versions won't be compatible. Use the same version for all JavaFX dependencies.

i went to file> project structure > libraries > add library "java", opened the path where i installed my javafx > lib and chose javafx.media, then module-info.java could recognize it.

You should have your IDE re-import the updated maven project and auto-resolve the dependencies, not add the libraries manually to the IDE project.

For Idea, see:

For other IDEs the procedure will differ.


If you want to use WebView, or FXML or Controls, then use the sample module and dependency names here.

All steps for usage of these modules are the same as for the javafx.media module, only the names change.

Sample module names:

javafx.web
javafx.swing
javafx.fxml
javafx.media
javafx.controls

Sample dependency artifact names:

javafx-web
javafx-swing
javafx-fxml
javafx-media
javafx-controls
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks for your answer! I have tried that and i just changed the dependency to what you wrote and i got this error. – Malek Anas Sep 22 '20 at 20:40
  • Could not find artifact org.openjfx:javafx.media:pom:15 in central (https://repo.maven.apache.org/maven2) – Malek Anas Sep 22 '20 at 20:40
  • 1
    Sorry was a typo, artifactId should be `javafx-media`, not `javafx.media`, I edited the answer to fix it. See https://search.maven.org/artifact/org.openjfx/javafx-media/15/jar – jewelsea Sep 22 '20 at 20:48
  • 1
    Edited the answer to add more info on what not to do based upon your updated question. – jewelsea Sep 22 '20 at 21:00
  • Thanks ! the pom file now recognizes the dependency but i still can not require the javafx.media in my module-info.java – Malek Anas Sep 22 '20 at 21:06
  • Thanks a lot! Now it is solved, i actually needed to add lib to my project, i went to file> project structure > libraries > add library "java", opened the path where i installed my javafx > lib and chose javafx.media, then module-info.java could recognize it. – Malek Anas Sep 22 '20 at 21:21
  • Updated answer to note that you should have your IDE re-import the updated maven project and auto-resolve the dependencies, not add the libraries manually to the IDE project. – jewelsea Sep 22 '20 at 21:49
-1

I found my mistake, i needed to add javafx.media from directory where i installed javafx, like this C:\Program Files\openjfx-14.0.2.1_windows-x64_bin-sdk\javafx-sdk-14.0.2.1\lib

to achieve that i needed to go to file > project structure

enter image description here

then i chose the javafx.media.jar

enter image description here

after doing this module-info.java could recognize the module.

Malek Anas
  • 91
  • 1
  • 7
  • 2
    I don't advise doing this. You are using Maven and Intellij. Have them work together. Don't manually import dependent libraries, instead create [import your maven project definition in Idea](https://www.jetbrains.com/help/idea/maven-support.html#maven_import_project_start). If you edit the maven project definition, then [ask Idea to update your dependencies from Maven](https://stackoverflow.com/questions/16992255/how-can-i-make-intellij-idea-update-my-dependencies-from-maven/16992262). – jewelsea Sep 22 '20 at 21:42
  • 2
    Also, with this answer, you are *still* mixing versions of JavaFX libraries, which you *should not do*. The different versions are not guaranteed to be binary or semantically compatible, so, while it *might* work, a multitude of things could break if you do this. – jewelsea Sep 22 '20 at 21:44