16

I have a JavaFX project that I would like to build as a Jar-file. However, when I attempt to do so, I get an error.

Error:Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK

I found a similar problem on here from last year, but it seemed like they concluded nothing.

Mr. Nielzom
  • 317
  • 1
  • 2
  • 11
  • First, I downloaded the javaFx sdk from https://gluonhq.com/products/javafx/, then I created a bat file and I included something like this: java --module-path PATH_TO_JAVAFX_SDK --add-modules=javafx.controls,javafx.fxml -jar PATH_TO_YOUR_JAR_FILE – Leeuw Aug 02 '23 at 00:07

2 Answers2

12

This happens because either you have many JDKs installed and compiling by another and running by another or you are using the Javafx Application jar feature when creating artifacts in Intellij which is unfortunately broken. Before proceeding with the below steps make sure that you are compiling with and running with the same JDK version. Here is you fix it:

1 - Create a Launcher class:

The Launcher class is going to call the main JavaFx class from which your appliaction runs. Choosing to make the Jar directly through the Main class is going to error out giving the following error:

    Error: Could not find or load main class Main
    Caused by: java.lang.ClassNotFoundException: Main 

Your Launcher class should look something like this:

    public class Launcher {
        public static void main(String[] args) {
            MainGUI.main(args);
        }
    }

2 - On to building the Jar

  1. You probably still have a META-INF folder from the previous build so delete it.

  2. Build the project as a JAR:
    File->Project Structure -> Artifacts -> "+" -> JAR-> from modules with dependancies..

  3. Choose the Launcher class for you main and check "copy to the output directory and link via Manifest" and Press Ok

  4. Press Apply then OK.

  5. go to Build -> Build artifacts-> Rebuild

AM429
  • 306
  • 3
  • 11
  • 1
    Here is an excellent video explaining the second option: https://www.youtube.com/watch?v=HGHu-SzL-5E&ab_channel=AlexHorea – Dan A.S. Dec 02 '20 at 12:01
  • I know I'm reviving an old topic (bad practice) but I just wanted to say a huge "Thank you!" because you saved me from pretty much wasting my work on a seemingly unpackage-able program. – Kotaka Danski Nov 02 '21 at 10:13
  • It work! open without adding the modules and no error! thanks – FarshiD Naqizadeh May 19 '23 at 22:05
1

In the JetBrains website I found a good article about, Package JavaFX applications which was really helpful. In the #troubleshoot section it says that,

Error:Java FX Packager: Can't build artifact – fx:deploy is not available in this JDK

The fx:deploy task was a part of the Ant plugin that was formerly distributed in ant-javafx.jar as a part of Java Packager. The Ant plugin is not included in jpackage in the current JDK versions.

If you're using a JDK build of version 9 and later, use third-party solutions for packaging. For example, refer to section Runtime images in the JavaFX official documentation.

Tharindu Sathischandra
  • 1,654
  • 1
  • 15
  • 37
  • 1
    This is a good summary. Additionally, if somebody is interested in packaging a Java (or JavaFX) application as a native application (e.g. an Windows exe, Linux rpm or deb package, or OS X dmg installed app), then consider using the [jpackage](https://www.baeldung.com/java14-jpackage) utility implemented as an [incubating feature in JDK 14+](https://openjdk.java.net/jeps/343). For certain applications, this may provide a better requirements fit than the current options documented for JavaFX [runtime images](https://openjfx.io/openjfx-docs/#modular). – jewelsea Aug 27 '20 at 18:38
  • @jewelsea thanks for mentioning about jpackage. Could you please help me figuring out this; I was trying to create an native executable for a JavaFX modular Maven project. With using [javafx-maven-plugin](https://github.com/openjfx/javafx-maven-plugin) I could create a runtime image with a launcher script & then by packaging it with jpackage, I got an bundled exe setup which works fine. But, it installs that launcher script. But what I need is to install running exe file. Any suggestion how to do it? – Tharindu Sathischandra Aug 30 '20 at 11:03
  • Ask a new question Tharindu. By doing so, multiple people may answer, you will have a better chance of finding someone with the appropriate knowledge to create a good answer for you and there will be a record which somebody can search to get an answer to a similar question. – jewelsea Sep 01 '20 at 18:06
  • Kind of lame that what is supposed to be an IDE cannot even create a final output of a project. I mean, if it only works for Java 8 and below, that is useless in most cases. I do not understand why IntelliJ did not include a solution (whether their own or a third-party one) for Java 9+. Unless JavaFX is dying or something and IntelliJ practically gave up on it. – Damn Vegetables Feb 02 '22 at 01:34