0

I am trying to generate a jar file by embedding within it the javaFx dependencies needed to run my program.

Below is my pom.xml file that includes the shave plugin which should do exactly that.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>SoftwareRtf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SoftwareRtf</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>11</version>
            <classifier>win</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>11</version>
            <classifier>linux</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>11</version>
            <classifier>mac</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-media -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>20-ea+9</version>
        </dependency>

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


        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.softwarertf.MenuExample</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Later from the command line I ran: mvn package

In doing so, two files are generated: original-SoftwareRtf-1.0-SNAPSHOT.jar and SoftwareRtf-1.0-SNAPSHOT.jar

That by executing them using the command: java -jar .... produce this error

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

Using the following command I was able to see what is contained in the jar file and I see the JavaFx files inside them.

mvn dependency:tree -Ddetail=true

com.example:SoftwareRtf:jar:1.0-SNAPSHOT
\[INFO\] +- org.openjfx:javafx-controls:jar:17.0.2:compile
\[INFO\] |  +- org.openjfx:javafx-controls:jar:mac-aarch64:17.0.2:compile
\[INFO\] |  - org.openjfx:javafx-graphics:jar:17.0.2:compile
\[INFO\] |     +- org.openjfx:javafx-graphics:jar:mac-aarch64:17.0.2:compile
\[INFO\] |     - org.openjfx:javafx-base:jar:17.0.2:compile
\[INFO\] |        - org.openjfx:javafx-base:jar:mac-aarch64:17.0.2:compile
\[INFO\] +- org.openjfx:javafx-media:jar:20-ea+9:compile
\[INFO\] |  - org.openjfx:javafx-media:jar:mac-aarch64:20-ea+9:compile
\[INFO\] +- org.openjfx:javafx-fxml:jar:17.0.2:compile
\[INFO\] |  - org.openjfx:javafx-fxml:jar:mac-aarch64:17.0.2:compile
\[INFO\] +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
\[INFO\] |  +- org.opentest4j:opentest4j:jar:1.2.0:test
\[INFO\] |  +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
\[INFO\] |  - org.apiguardian:apiguardian-api:jar:1.1.2:test
\[INFO\] - org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
\[INFO\]    - org.junit.platform:junit-platform-engine:jar:1.8.2:test

I don't understand what I am doing wrong, could it be some necessary module that was not included? Any input is welcome. Thank you!

5
  • The dependency tree is the dependency tree for your app, not the contents of a shaded jar. The shaded jar does not include test code or test dependencies or compile only dependencies but does include resources, which are not dependencies. So he shaded jar is not the same as the dependency tree. To see what is in the jar, run jar tvf on it. Commented Dec 10, 2022 at 21:38
  • “could it be some necessary module that was not included?” -> yes a module must be a jar file or encoded in a linked image. The jar file can only contain a single module, so it is impossible for a shaded jar to contain multiple modules, and JavaFX needs multiple modules to work. Commented Dec 10, 2022 at 21:45
  • Only classify dependencies if you decide to continue shading regardless. If you do that then classify all JavaFX dependencies not just some and provide all needed classifiers for all required JavaFX dependencies regardless of transitivity. Commented Dec 10, 2022 at 21:51
  • I do not understand why every day people ask this same question. No documentation recommends using shade to package a JavaFX application. For simple apps like this, with only JavaFX dependencies, use the javafx-maven-plugin to create a zip of a jlink image for distribution, it is simple, reliable and easy Commented Dec 10, 2022 at 21:52
  • Don’t mix JavaFX versions for different JavaFX modules in the same app. Don’t use ea versions. Commented Dec 10, 2022 at 21:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.