12

My JavaFX application is running perfectly well from source but When I'm compiling to a single jar file I get error :

Error: JavaFX runtime components are missing, and are required to run this application.

I'm using Maven as my repository manager and My install with Maven is successful.

Note: In my IntelliJ build artifact I can see that IntelliJ include JavaFX and all its libraries

Lii
  • 11,553
  • 8
  • 64
  • 88
Hilary Okoro
  • 281
  • 1
  • 3
  • 7
  • Duplicated of [Maven Shade JavaFX runtime components are missing](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing) – José Pereda Jul 05 '19 at 08:43
  • This answer is detailed and helped me: https://stackoverflow.com/a/70809214/10946427 – Dmitriy Sep 24 '22 at 17:19

3 Answers3

28

a) For my maven project the trick was to use an extra starter class that does not inherit from javafx.application.Application:

public class GUIStarter {

    public static void main(final String[] args) {
        GUI.main(args);
    }

}

The JavaFx dependencies are included in my pom.xml as follows:

<!-- JavaFx -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>12</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>12</version>
        </dependency>

         <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>12</version>
        </dependency>

In order to include fxml files in your build, you might need to define them as resource:

!-- define JavaFx files as resources to include them in the jar -->
<resource>
      <directory>${basedir}/src/main/java/foo/baa/view</directory>
      <filtering>false</filtering>  
      <targetPath>foo/baa/view</targetPath>            
</resource>

b) As an alternative, an extra maven plugin "javafx-maven-plugin" could be used to build the javafx appication, see example project at https://openjfx.io/openjfx-docs/#maven (This did not work for me. I have several pom files and want to reference the javafx dependencies in a sub project. My main class could not be found by the javafx-maven-plugin.)

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • 1
    The extra starter class worked for me. Using Netbeans 11.3, I created a new project using "FXML JavaFX Maven Archetype (Gluon)" which could build but not run. The question is "why does this work?" – Doc May 02 '20 at 13:35
  • 2
    @Doc See info about "sun.launcher.LauncherHelper" here: https://mail.openjdk.java.net/pipermail/openjfx-dev/2018-June/021977.html – Stefan May 02 '20 at 14:11
  • @Stefan, Can you please give me a sample project link where it works? – Md. Asaduzzaman Jun 20 '23 at 09:36
  • @Md. Asaduzzaman Nope. I do not use Java any more and have nothing online from the old projects. Good luck. – Stefan Jun 20 '23 at 10:02
0

STEP 1 - In your src. Step 1 add "extra" main I named Launcher as you can see in Step2 image.

STEP 2 - After in the "extra" class create a main and in that call the main of the MainClassJavaFx Step 2

STEP 3 - From project structure add new artifact and select the "extra class" as main and relocate the MANIFEST folder to src and not resources. Step 3

STEP 4 - Add all dll from javafx sdk from your folder Step 4

STEP 5 - Build artifact

This should works.

-1

In Java 11 the JavaFX components have been removed into their own SDK, so when you run it won't be able to find them in the runtime.

The JavaFX page has the instructions to get it going: https://openjfx.io/openjfx-docs/#install-javafx

In short, you have to compile / run by adding the javafx modules in, either as options passed on the command line, or using a modular project.

I found that the modular project with Maven and IntelliJ worked best for me. https://openjfx.io/openjfx-docs/#IDE-Intellij

In the modular method you have a module-info.java file that describes all the modules that your project "requires", and allows you to "open" them to other modules. If you have a bunch of Maven dependencies you have to add them in the requires list too though. (IntelliJ can make this easy - find the error about no requires in the code and alt-enter)

Once everything was working with modules etc, I had to make a Fat Jar using I think the Maven shade plugin, to put everything together. Then it would work running the jar from the command line.

However, after getting my Java 11 code @#$%@# working after 2 days of pain, I went back to Java 8 (using correto SDK for latest version) as Java 11 doesn't have packaging and IntelliJ can't do it for you.

geometrikal
  • 3,195
  • 2
  • 29
  • 40
  • JavaFx is in my module – Hilary Okoro Jul 05 '19 at 23:07
  • I have resolved that issue. Maven is repository is not including my libraries during compiling to a single jar file. I had to stop using maven for the compilation and move my source to different project then manually compile it myself – Hilary Okoro Aug 03 '19 at 10:47