Build JavaFX runnable JAR with IntelliJ IDEA (and Ant)

For years now I’ve been using NetBeans and I’m satisfied about it. But I wanted to give IntelliJ IDEA a try so I decided to test it by creating some JavaFX apps. And if you’re following the news, JetBeains has released IntelliJ IDEA 12. The thing is IntelliJ doesn’t have specific JavaFX features so I really wanted to see how to deal with it. Since JavaFX 2 is a part of the JDK 7 now, you won’t face any problem according like the JavaFX classes. The “problem” is I really have my habits on NetBeans and it is kind of difficult to throw them away. And one of my first problem was: how do I create a runnable JAR for my JavaFX app? Well I know the stuff about Ant tasks, the JavaFX packager in theory. But I didn’t had to really know them in details because NetBeans really does everything for you.
To be very clear: I won’t judge neither NetBeans, neither IntelliJ. Both have their pros and cons and both are great products. The subject here is really to focus on how to create a runnable JavaFX JAR in IntelliJ using Ant. And because I am really a newbie to IntelliJ, I hope this post could help others.

The Ant build script

First thing we have to do is to create the Ant build script. Go to Build > Generate Ant Build …
Then create the script by giving it a name. I personally decided to make it as simple as possible.
After that, I replaced the whole generated content of the build script by the following one:

If you’re familiar with Ant, this is pretty easy: I define some basic targets for cleaning and compiling. The most interesting part is the target build. It is there that the final jar for our application is done using some new tags like:

fx:application that represents our JavaFX app, where you define the application class, the fallback one and so on ;

fx:jar that will create the final JAR by assembling the classes, convert CSS files into binary format if wanted and so son.

Of course there are other tags like fx:deploy, fx:signJar, etc. But here we only wanted to build a simple JAR without any particular configuration.

Try it

Once your build script is ready you just have to execute the build target to get your runnable JAR. To do so, at the right of IntelliJ click on Ant Build and then on the + in order to add your build script.
Choose your file:
And then you will be able to execute targets you want.

5 Responses to Build JavaFX runnable JAR with IntelliJ IDEA (and Ant)

It seems this plugin is for JavaFX 1.2.1, which EOL is announced for December, 20th. Moreover it builds your project for JavaWebStart which is not the goal of this article. But if you wanna use Maven with JFX2, you just have to declare the systemPath of your JavaFX runtime. There are plenty articles explaining how to do that, like: http://www.zenjava.com/2012/02/17/javafx-and-maven/

Might be stupid of me to ask, but the instrunctions mentioned are only needed if you are building jar for project which has GUI made through FXML? as simply using artifacts for getting single jar seems to work fine for me or this is just coincidence ?