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+) ?
Open the Maven Projects window ( View -> Tool Windows -> Maven ) and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.
To create a JAR file from a Maven project in IntelliJ IDEA, go to the Maven Tool Window (View → Tool Windows → Maven), expand your project in the tree, expand Lifecycle, and then double-click on package. Maven will compile your package, and the compiled JAR file will be written to the target/ directory.
jar'. The resulting 'jar' file contains the compiled java class files as well as the files from src/main/resources . Usually there is no need to mentioned the 'maven-jar-plugin' explicit cause it's bound to the Maven Build Life Cycle. For full documentation, click here.
2019 Update for Recent JavaFX Versions
As of Java 11, JavaFX has been decoupled from the JDK, so JavaFX libraries are no longer automatically bundled together with a JDK install (as they were for instance in the Oracle JDK 8 distribution). Instead, the JavaFX system is now designed as a set of separate libraries independent from the JDK, which can be downloaded from a repository by a tool such as Maven for use by your application.
openjfx.io have a very short tutorial on using JavaFX 11+ with Maven:
This tutorial recommends use of the OpenJFX JavaFX Maven Plugin and provides the following sample maven pom.xml project file for a "hello, world" style application.
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.openjfx</groupId> <artifactId>hellofx</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>12.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.2</version> <configuration> <mainClass>HelloFX</mainClass> </configuration> </plugin> </plugins> </build> </project>
Note that the org.openjfx JavaFX maven plugin used for Java 11+ is different from the com.zenjava JavaFX maven plugin used for Java 8 and 9, so use the appropriate one for your Java version.
Suggested Approach
For building JavaFX apps with Maven:
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:
jfxrt.jar
would be able to find them).lib
directory may not work against a later JDK version, such as JDK 8.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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With