Home > Enterprise >  JavaFX Maven shaded jar fails to open on double click but can run from terminal
JavaFX Maven shaded jar fails to open on double click but can run from terminal

Time:04-25

This is the Maven shaded plugin I'm using in my pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>3.2.4</version>
          <executions>
               <execution>
                 <goals>
                   <goal>shade</goal>
                </goals>
                 <configuration>
                     <shadedArtifactAttached>true</shadedArtifactAttached>
                     <transformers>
      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                          <mainClass>com.myApp.Main</mainClass>
                        </transformer>
                 </transformers>
              </configuration>
          </execution>
     </executions>
</plugin>

It loads absolutely fine from the terminal with java -jar myApp so I have no idea why it doesn't work when simply double clicking. The error message from the finder I get with double clicking is:

The Java JAR File "myApp-1.0-SNAPSHOT-shaded.jar" could not be launched

I've created several different apps all with the exact same issue; they run ok as a shaded jar in terminal but fail to launch on a double click so I don't think it's anything unique to any particular app.

EDIT: I'm on Macos and also I have no problem launching any other jars with double click, only these specific jars I created myself with Maven shade.

CodePudding user response:

Do you mean the eclipse terminal or the operating system terminal?

Sorry for answering with a question, but I can't comment...

Jfx sometimes acts mysterious.

Like the fact, that it don't likes it, when the class, in which your main() is located, is inheriting from another class. Please try to create an own class only for your main().

It may sound a little bit strange, but please create an app with maven shade plugin, without javafx library and test, if it runs.

CodePudding user response:

Today I came ahead the same problem, but with a JFX-gradle project, and I remembered how I solved this problem some time ago in a JFX-maven project.

This error occurs only with JFX-Projects and it took me hours to find it.

Please add this to your pom.xml

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

Here is a pom.xml example of one my JFX-Projects:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wedasoft</groupId>
<artifactId>FxMultiMessageSender</artifactId>
<version>0.0.2</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <javafx.version>17</javafx.version>
</properties>

<dependencies>
    <!-- javafx -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>${javafx.version}</version>
    </dependency>
    <!-- wedasoft libraries -->
    <dependency>
        <groupId>com.wedasoft</groupId>
        <artifactId>wedasoftLibraries</artifactId>
        <version>0.0.4</version>
        <exclusions>
            <exclusion>
                <groupId>org.openjfx</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- database libraries -->
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.36.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.8</version>
            <executions>
                <execution>
                    <!-- Default configuration for running -->
                    <!-- Usage: mvn clean javafx:run -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>com.wedasoft.FxMultiMessageSender.FxMultiMessageSenderMain</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.wedasoft.FxMultiMessageSender.FxMultiMessageSenderMain</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Related