Reputation: 101
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;
}
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.
Upvotes: 6
Views: 11859
Reputation: 159341
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.
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>15</version>
</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
Upvotes: 6
Reputation: 101
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
then i chose the javafx.media.jar
after doing this module-info.java could recognize the module.
Upvotes: -1