23

I'm using IntelliJ IDEA, and I have my JavaFX application ready for deployment. The problem is that when I generate the JAR file, it won't run, when I run it in the command line, I get an Exception, FXMLLoadException, although the project is working perfectly in my IDE.

Ant tasks end with errors, after 15 minutes of building, I really don't understand what's the problem exactly.

So my question what are the right steps to deploy a JavaFX application the right way, any tutorial or guide will be welcome.

Jean-Baptiste-B
  • 325
  • 1
  • 3
  • 9
  • 1
    Could you post the last few lines of your ant log? The `FXMLLoadException` sounds like your `.fxml` file is not included in your `.jar`. You might want to try unpacking the jar (`unzip jarfile.jar`) and search for your `.fxml` file. Anyway, I'm using gradle with [Travis CI](https://travis-ci.org) and it allows deploying to many different deploy providers. – michaeln May 10 '15 at 22:14

3 Answers3

29

A Java application can be packaged in various ways. Please go through Java Packaging Overview to find everything about it. One of the packaging is a self-contained Java application.

There are different ways to create these packages :

  • Use the javapackager tools that comes with your JDK
  • JavaFX Ant Tasks
  • JavaFX Maven Plugin for a maven project

Self-contained application is one the ways how your application can be packaged and is platform specific. The bundle contains :

  • The application package
  • A private copy of the JRE

A list of available bundles can be found here.

Let us check out the various tools available at our disposal and how to use them:

JavaPackager Tool

JavaPackager Tool is the most basic tool and helps you to compile, package, sign, and deploy your Java(FX) applications, without writing any additional scripts. The javapackager.jar file is located in the bin directory of the JDK installation.

The list of commands, that can be used with it is available here.

JavaFX Ant Tasks

JavaFX Ant Tasks helps you package your application by just creating a build.xml file for your project.

A set of examples on how to use ant scripts for your project can be found here.

The list of commands that can be used for with it is available here.

JavaFX Maven Plugin

JavaFX Maven Plugin leverages the usage of packaging java application to the maven platform. You can use it to package your maven based java application by adding a plugin to the project.

This plugin IMHO, if the easiest to use out of the three. It is a very nicely written, easy to understand tool and has extensive documentation.

JavaFX Gradle Plugin

JavaFX Gradle Plugin is also from the author of the maven plugin. It has all the features that the maven plugin has, but for Gradle :)

Further Reading :

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • 2
    Thx, can you update "JavaFX Maven Plugin" the link is broken. I never used maven before and I don't get any information for noobs (like me). :-/ – Martin Pfeffer Sep 11 '15 at 22:57
  • 2
    JavaFX Maven Plugin works great for me, and so easy to use. just add plugin to pom, and one command e.g. "mvn jfx:native" – jack jin Jul 06 '16 at 04:03
  • Note the `javapackager` tool was deprecated and replaced by `jpackage`, see: [JEP 392: Packaging Tool](https://openjdk.java.net/jeps/392). – jewelsea Sep 09 '21 at 12:27
  • 1
    The JavaFX Maven Plugin link here is obsolete and broken and has been replaced by: [the gihub hosted openjfx javafx-maven-plugin](https://github.com/openjfx/javafx-maven-plugin), – jewelsea Sep 09 '21 at 12:32
  • Some of the blog links in this post are also now broken. – jewelsea Sep 09 '21 at 12:34
  • 2
    The most comprehensive and, currently, up-to-date resource (though not truly comprehensive) for this topic is [the openfjx.io documentation](https://openjfx.io/openjfx-docs/). – jewelsea Sep 09 '21 at 12:35
1

Oracle has an extensive guide about packaging on their website for JavaFX 2 and for JavaFX 8.

If you need help for your specific problem, post the code and the stacktrace of the error you get.

Roland
  • 18,114
  • 12
  • 62
  • 93
1

If you use Maven, another very smart approach is to use maven-shade-plugin (link). It builds one big fat jar with all dependencies inside which you can execute directly without any further things to do. You can use this in combination with javafx-maven-plugin. I also tried different approaches and played around a long time and this solution was the only one which really works. Additionally, it was easy to set up.

Here is what you have to add to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>your.package.name.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>your.package.name.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Change your package name inside mainClass for shade and also javaFx plugin and you are done. Now you can build your Application as always with mvn package.

Mark Kowalski
  • 264
  • 2
  • 9
  • The zenjava JavaFX Maven Plugin referenced here is obsolete and broken and has been replaced by: [the gihub hosted openjfx javafx-maven-plugin](https://github.com/openjfx/javafx-maven-plugin), – jewelsea Sep 09 '21 at 12:38
  • For recent JavaFX applications (e.g. JavaFX 11+), creation of a jar using maven shade is not recommended. JavaFX 11+ is designed to be run as part of a modular distribution, which isn't really designed to be compatible with a shaded jar. See the [openjfx documentation](https://openjfx.io/openjfx-docs/) for some alternate options for runtime image generation. – jewelsea Sep 09 '21 at 12:41