0

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.

Hasen
  • 11,710
  • 23
  • 77
  • 135
  • Personally, I recommend ditching the shaded jar approach and using either a [jlink zip image](https://github.com/openjfx/javafx-maven-plugin#javafxjlink-options) or [jpackage installer](https://github.com/dlemmermann/JPackageScriptFX). If you wish to continue with the shade approach, see [this JavaFX shade guide](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing). I have no specific advice on how to fix the exact issue you are currently experiencing. – jewelsea Apr 22 '22 at 18:06
  • I tried this (following the shade guide I referenced). The shaded jar ran fine both from the command line and on double click in Finder. Test system JavaFX 17.0.2, OpenJDK 17.0.2, OS X (Intel) 12.3. I guess your mileage will vary. As it worked for me, I still have no specific advice for you. – jewelsea Apr 22 '22 at 18:34

2 Answers2

0

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.

David Weber
  • 173
  • 9
  • My question probably won't get any more replies now you've answered but I understand you couldn't comment. I hope you can aid further in these comments. I mean any terminal, as long as I switch to the directory the jar is in. But since it works in terminal that's not the issue. The issue is with double clicking on the jar itself. – Hasen Apr 22 '22 at 10:49
  • By creating a shade plugin without javafx you mean just a hello world or something? When I create a new project with Maven in intellij it already has javafx with it though. I don't see exactly why it works fine in the terminal but not with a double click? I mean what could be going on that is different there. – Hasen Apr 22 '22 at 10:51
  • @Hasen *"I mean any terminal, as long as I switch to the directory the jar is in."* What happens when you run from the terminal if you are not in the directory the jar is in? I.e. `java -jar /full/path/to/file.jar` – James_D Apr 22 '22 at 11:47
  • Please check this source out. This was a problem, that I encountered with JFX a few weeks ago. https://www.pixelstech.net/article/1463037958-Run-executable-jar-on-double-click-on-Windows#:~:text=Ideally%2C%20following%20steps%20can%20be,jar%20file.&text=If%20somehow%20this%20method%20doesn,the%20problem%20should%20be%20resolved. – David Weber Apr 22 '22 at 11:57
  • @James_D Yeah it works fine like that too, I just meant the path has to be correct for it to work. Apologies for the confusion. Everything works fine except double clicking the jar directly. – Hasen Apr 22 '22 at 12:16
  • @David Weber I'm actually running it on Mac, that's why I have terminal. I can't comment as to whether the jar would run with a double click on windows. Also I don't have a problem with launching jars on double click generally, only THESE specific jars created with maven shade. I just checked another random jar and it runs fine. – Hasen Apr 22 '22 at 12:18
  • @Hasen, I am really sure, that this will solve your problem. I remembered the solution again... Check my answer out, no joke! There is an example pom.xml of one of my jfx maven projects. Make sure to read the full answer! https://stackoverflow.com/questions/71989018/exe-using-launch4j-does-not-work-javafx-project-jar-files-need-to-be-ran-from/71990054#71990054 – David Weber Apr 24 '22 at 19:59
0

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>
David Weber
  • 173
  • 9