1

I have been trying to fix this error for the past few hours but didn't manage to do so.

First of all, I created a Maven Project where I added the needed JavaFX dependencies (POM.xml):

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>STTGui</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<build>
    <!-- Maven Plugin configuration is required, without it an internal java error is thrown -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>12</release>
            </configuration>
        </plugin>

        <!-- Maven Shade Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <!-- Run shade goal on package phase -->
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- add Main-Class to manifest file -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>speechToText.userInterface.MainEntry</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>speechToText.userInterface.MainEntry</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>maven-cognitiveservices-speech</id>
        <name>Microsoft Cognitive Services Speech Maven Repository</name>
        <url>https://csspeechstorage.blob.core.windows.net/maven/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.microsoft.cognitiveservices.speech</groupId>
        <artifactId>client-sdk</artifactId>
        <version>1.6.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>12.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-media -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>12.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-graphics -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>12.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>12.0.2</version>
    </dependency>

</dependencies>
</project>  

I also added module-info.java:

module SSTGui {
    requires javafx.controls;
    requires javafx.fxml;
    requires client.sdk;
    opens speechToText.userInterface to javafx.fxml;
    exports speechToText.userInterface;
}

When I execute the JavaFX application inside my IDE all works fine, when I build a Jar using maven -> lifecycle ->package the jar builds but when executing it I get the following error:

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

Idea:
1) Install OPENJDK because Java JDK doesn't contain runtime components for JavaFX anymore -> Didn't result in any change.
2) Use the OpenJFX SDK of GLUON instead of Maven, didn't solve it either, same problem.

I guess somehow that the dependencies get not packed in the JAR that's why it might now work, but I couldn't solve it, any idea?

EDIT I added `maven shade plugin to create a fat jar:

<!-- Maven Shade Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <!-- Run shade goal on package phase -->
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- add Main-Class to manifest file -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>speechToText.userInterface.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>  

But after the built is finished and two jars are created: original-name.jar and name.jar When I execute the name.jar I get the following error:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

When executing original-name.jar I get:

no main manifest attribute, in original-STTGui-1.0-SNAPSHOT.jar

To solve this I tried implementing a Helper main-class which doesn't extend from Application which starts my native main:

package speechToText.userInterface;

public class MainEntry {

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

I also changed the <mainClass> attribute in my maven-shade-plugin to this class and also the attribute in javafx-plugin.

Alan
  • 589
  • 5
  • 29
  • You are right, the jar doesn't include the JavaFX dependencies. You can use the shade plugin for that (see [this question](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing/)), or even better, since you have a modular app, you can run `mvn javafx:jlink` to create a custom image (see [doc](https://github.com/openjfx/javafx-maven-plugin#usage)). – José Pereda Aug 21 '19 at 09:03
  • @JoséPereda using `mvn javafx:jlink` throws: Error: automatic module cannot be used with jlink: client.sdk (my cognitive speech service) + Failed to execute goal org.openjfx:javafx-maven-plugin:0.0.3:jlink – Alan Aug 21 '19 at 09:11
  • Then you can't use jlink directly. There are more complex options to create a module out of non-modular dependencies, but for now I guess you could just do a fat jar. – José Pereda Aug 21 '19 at 09:16
  • @JoséPereda I tried the shade plugin, and updated my question. First of all two JAR's are generated, while I don't know why one is named STT...jar and the other original-STT...jar and after executing STT..jar I get `Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes` – Alan Aug 21 '19 at 09:31
  • For starters, you need to use a Launcher class (see question I linked before). Can't say what is SST, you should get a "fat" jar (in terms of MBs), that you can run with `java -jar yourFatJar.jar`. Are you using AdoptOpenJDK by any chance? – José Pereda Aug 21 '19 at 09:35
  • STT is just the name of my jar STTGui-1.0-SNAPSHOT.jar , I use the normal `JDK 12` by oracle: https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html With the launcher class you mean by creating a new Main class and launching from there the Class which extends my Application? – Alan Aug 21 '19 at 09:39
  • Yes, a new Main class that doesn't extend Application – José Pereda Aug 21 '19 at 09:45
  • @JoséPereda I tried that and changed the mainClass attribute of the shade plugin to the newly created Main still the same problem. I''ll update my code – Alan Aug 21 '19 at 09:47
  • Updated POM.xml and code – Alan Aug 21 '19 at 09:54
  • The MainEntry solves a different issue that you would get once you solve the manifest problem with this other [answer](https://stackoverflow.com/a/6743609/3956070). – José Pereda Aug 21 '19 at 09:55

1 Answers1

2

Based on the comments, this answer collects a few hints that were required to solve the issue.

Jlink error

About distribution: ideally you should use jlink to distribute your application, creating a custom runtime image. But running mvn javafx:jlink gives:

Error: automatic module cannot be used with jlink: client.sdk

Even if your application is modular, it doesn't mean that all the dependencies are modular too, so mvn javafx:jlink may not work. However, there are a few options to do this using the modular approach:

Based on this question Is there a way to add maven dependencies while using the maven-jlink-plugin?, you could try to create a module out of the failing client.sdk automatic module, but this is not trivial.

Another option to explore: use jpackage (early access), that will let you create an installer of your project, even if it is not modular. However this requires an early version of JDK 14...

At this point, the classic solution of an uber/fat jar seems the easiest approach.

JavaFX dependencies error

However, after using mvn package and running the jar:

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

Basically:

  • The jar doesn't contain the JavaFX dependencies
  • Even if it did, it won't work, if the main class extends from Application.

This has been solved in this question Maven Shade JavaFX runtime components are missing.

So you need to add the maven-shade-plugin in order to bundle the JavaFX dependencies, and you need to add a Launcher or entry point class that doesn't extend from Application.

Manifest error

About the error:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

This other question “Invalid signature file” when attempting to run a .jar gives you the solution to fix the issues with the shade plugin, by adding a filter that excludes the manifest signature files.

Once all the errors are solved you should be able to run:

mvn package
java -jar target/yourJar.jar
José Pereda
  • 44,311
  • 7
  • 104
  • 132