I'm setting up a maven project, that is going to use JavaFX. Since I've heard JavaFX does not come with all versions of Java, I downloaded and put the jfxrt.jar
file in a lib
directory in my project.
1) How do I specify that the dependency (ie., JavaFX) should not be downloaded, but which is located in lib
?
2) Does this then mean the project can be built on any machine with JDK (and not necessary JDK 1.7 - update 9+) ?
Suggested Approach
For building JavaFX apps with Maven:
- Use a maven JavaFX plugin AND/OR
- Use Java 8 don't specify any kind of Maven dependency for JavaFX.
Unlike Java 7, with Java 8 it is unnecessary to set JavaFX as a maven dependency, because JavaFX is on the standard Java runtime classpath (similar to the way Swing is today).
Opinion
I think the approach in your question of placing the jfxrt.jar
file in a project lib
directory is flawed.
The reasons it is flawed are:
- JavaFX also includes native libraries, so you would need to include them as well (in a location where
jfxrt.jar
would be able to find them).
- A given version of JavaFX will only be certified to operate against the version of the JDK it is shipped with, so the JavaFX runtime you place in your project's
lib
directory may not work against a later JDK version, such as JDK 8.
- Future versions of the JDK such as JDK 8 will include JavaFX on the default classpath of the JDK, so you may get conflicts with the version you place in your
lib
directory.
In summary, JavaFX should be treated as part of the Java runtime system, not as a project library.
JavaFX System Dependency Sample
If you must use Java 7 and you don't want to use the JavaFX maven plugin, then you can take the less recommended approach:
- Reference the JavaFX library from the jdk (not your project
lib
directory) as a maven system dependency.
This sample is provided for Java 7 only and is not required (and will not work as is) for Java 8. Java 8, places the jfxrt.jar in ${java.home}/lib/ext/jfxrt.jar, which is on the default Java runtime classpath, making a system dependency for Java 8 irrelevant.
<dependency>
<groupId>javafx</groupId>
<artifactId>jfxrt</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>
If you package your application using a system dependency like this, then you need will need to write some execution instructions for your users so that they can add jfxrt.jar to the runtime classpath when running your application on Java 7. This is a very good reason not to use this approach.
If you choose to use the maven system dependency approach, you may also wish to use the maven antrun plugin to embed calls to the JavaFX ant tasks as in this sample. You may also want to use the maven enforcer plugin to ensure that your application is built against at least the minimum required version of Java containing a JavaFX version required by your application to work.