0

I am not able to create a workable FatJar with Maven in my current Project. I already consulted the common method to use when creating a FatJar with Maven and JavaFX. So I created a new Main Class that does not extend Application and referenced it in the Plugins instead of the "real" Main Class.

I consulted following Links already: https://www.reddit.com/r/javahelp/comments/ad1us7/creating_a_standalone_jar_of_a_program_using/ Maven Shade JavaFX runtime components are missing Build executable JAR with JavaFX11 from maven https://github.com/jesuino/fat-jar-javafx/blob/master/pom.xml

All of them suggest the solution I described above but still the created jar file does not open my Project instead it does nothing when opened.

This is my Maven 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>com.baeldung</groupId>
    <artifactId>core-java</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <configuration>
                        <mainClass>code.SuperMain</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-maven-plugin</artifactId>
                    <version>0.0.8</version>
                    <configuration>
                        <mainClass>code.SuperMain</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>code.SuperMain</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20211205</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

This is my newly created Main Class: package code;

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

And this the original:

package code;

import code.parser.JsonParser;
import code.controller.LevelController;
import code.controller.LevelOverviewController;
import javafx.application.Application;
import javafx.stage.Stage;
import code.logger.HTMLLogger;
import code.model.Game;
import code.model.GameLevel;
import code.model.MapGenerator;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * BoulderDash main class to initialize code.logger and start game
 * @author Inga Fabry
 */
public class Main extends Application {
    private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private static final String PATH = "src/main/resources/configurations/";

    /**
     * sets up code.logger to log exceptions
     */
    @Override
    public void init() {
        setUpLogger();
    }

    /**
     * parses level JSONS, creates levels and game from JSON data
     * starts levelControllers
     * @param stage as primary Stage for the LevelOverview
     */
    @Override
    public void start(final Stage stage) {
        JsonParser parser = new JsonParser();
        Game game = new Game();
        addLevelsToGame(parser, game);
        LevelController levelController = new LevelController();
        new LevelOverviewController(levelController, game, parser, stage);
    }

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

  ...and so on 
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • 1
    Quoting from the answer to the maven shade question which you reference: “even this widely accepted answer explains how can an uber/fat jar can be created on Maven projects, its use is discouraged, and other modern alternatives to distribute your application, like jlink, jpackage or native-image, should be used.”. – jewelsea Mar 02 '22 at 01:53
  • If you wish to continue despite that, then follow the not recommended steps in: [Can not run JAVAFX .jar File (many errors occurred)](https://stackoverflow.com/questions/71125172/can-not-run-javafx-jar-file-many-errors-occurred/71139922#71139922) – jewelsea Mar 02 '22 at 02:00
  • “it does nothing when opened” -> run it from the command line, provide both the full execution command and the output from the execution as text in the question, formatted as code. – jewelsea Mar 02 '22 at 02:03
  • 1
    Remove the `maven-surefire-plugin`, dependency, I don’t know why you would need that. – jewelsea Mar 02 '22 at 02:05
  • `PS C:\Users\Shutdown\IdeaProjects\g10_bughunter> java -jar .\target\core-java-0.1.0-SNAPSHOT.jar no main manifest attribute, in .\target\core-java-0.1.0-SNAPSHOT.jar` This is my cmd line in IDEA – Shutdown Mar 02 '22 at 16:53
  • The `maven-jar-plugin` can be configured to add a manifest with a main class. – trashgod Mar 02 '22 at 18:53

0 Answers0