简体   繁体   中英

Build standalone JAR with JavaFX 14 and FXML files

I have searched for hours trying to find what I'm looking for but there seems to be very little information regarding the latest version of JavaFX.

For a project, I am required to deliver a standalone executable JAR file that can run on any computer with java installed. I am using JDK 14 and JavaFX 14 and I am not using Maven or Gradle (although if one of those is necessary, there's no reason I can't use them).

My project runs fine in IntelliJ with VM options pointing to my JavaFX lib folder but I can't find how to properly build a JAR that can be distributed an executed without having javaFX the other system. ie I would like JavaFX, my FXML files, as well as my other dependencies (which includes JDBC) to be bundled within the JAR. Is this possible?

Please tell me if I need to supply any more information. Thanks guys.

I have found a Frankenstein solution!

IntelliJ can add external libraries to a regular Java project from the Maven repository without actually converting it to a Maven project (in fact, I was already using this for JUnit) I added the JavaFX dependencies as follows:

First I removed my existing dependencies and removed my VM options (these will no longer be required)

Project Settings -> Libraries -> + -> From Maven

and added:

  • org.openjfx:javafx-base:14
  • org.openjfx:javafx-controls:14
  • org.openjfx:javafx-fxml:14
  • org.openjfx:javafx-graphics:14

I had to click 'Apply' between each one or it wouldn't download them

I then created a JAR the normal way in IntelliJ:

Project Settings -> Artefacts -> JAR -> From modules with dependencies

added all the available elements to the output root

Build -> build artefacts

Upon running this, I got an error telling me it was looking for an FXML file so I copied my FXMLs to the same location and to my surprise, it ran flawlessly! I now plan to create a class within my project to generate the FXML files before my JavaFx code runs, that way, I can still distribute a single JAR.


Edit (Thanks @Slaw for your help)

Upon analysing the JAR, I found that my FXML files were indeed bundled inside. Accessing these is actually fairly simple, just change any FXMLLoader setLocation calls to:

myFxmlLoader.setlocation(MyClass.class.getResource("myFXMLFile.fxml"));

And it works flawlessly!

Thank you to everyone else for your suggestions too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM