JDK11/JavaFX: How do I make a fat jar without buil

2020-02-06 03:01发布

问题:

I think it goes without saying that I should be able to use Oracle's own JDK with JavaFX (from gluonhq) to build a distributable jar file that users can just USE.

After an exhaustive search, much reading (24 hours or more over the last few months)and finally this Google search query:

how to make a fat jar -maven -gradle -scala -eclipse -ant -docker -hadoop -netbeans -jerkar -phy -mozni -yogurt -pizza - throwing -python -bacon

I'm absolutely at the end of the road. Why on earth is this so much work? How can I build a JavaFX application and give it to people that want to actually use it without knowing anything else except how to use the application itself?

回答1:

This has been answered a few times already for Maven and Gradle. Build tools make things way easier than doing it on command line, and not only because of the dependency management.

Since you ask specifically about command line, there is already a full set of instructions documented for it here: https://openjfx.io/openjfx-docs/#modular.

Non modular App

The section Non-Modular from CLI covers JavaFX non-modular projects from command line, and gives you the whole set of instructions to create an old classic fat jar, where all the dependencies, including the JavaFX ones, are bundled all together.

There is a note that warns you not to use this procedure:

Warning: This is a discouraged tedious error-prone manual process that should be avoided by using the Maven's shade plugin or the Gradle's jar task, in case jlink is not applicable.

After you get the fat jar (it can be cross-platform), you can distribute it, and your user will need to have Java installed and run:

java -jar myFat.jar 

Modular App

The section Modular from CLI covers JavaFX modular projects from command line, and refers to the use of the jlink command, in terms of distribution, as it creates a custom image that you can send to your users. It is not a fat jar, but it will allow you sending a zip to your user that needs only to be unzipped and run like:

hellofx/bin/java -m hellofx/hellofx.HelloFX

In this case your user won't even need to have Java installed.

And with a little bit of extra work you can also create a batch, so you can run:

hellofx

However, if you still want to do a fat jar with a modular app, you can still apply the exact same instructions from the non-modular apps. In this case, you will probably have to remove the module-info.java file, as it doesn't really makes sense at this point.

Other options

You still have a few more options to distribute your application.

Custom Java+JavaFX image

Another option, covered in the same document, section Custom JDK+JavaFX image, explains how to create your own "JDK" that includes JavaFX. Then you will produce your jar as usual in Java 8 and you will be able to run it with:

/path/to/custom/java -jar myFat.jar

Note that there are already some JDK distributions that bundle JavaFX, like this one.

jpackage

jpackage tool is not there yet, but there is an early access: http://jdk.java.net/jpackage/, that is using Java 13-internal. The exiting documentation explains what are the command line options you need to produce a custom image or an installer.

Note that you can still use JavaFX 11 or 12 with this.

Build tools

And finally, you can still decide to use build tools (Maven or Gradle), that will really help you in many ways. See any of the linked questions above.