使用 Maven 和 JavaFX 创建 FatJar 时出现问题

发布于 2025-01-10 20:48:47 字数 7266 浏览 0 评论 0原文

我无法在当前项目中使用 Maven 创建可用的 FatJar。我已经查阅了使用 Maven 和 JavaFX 创建 FatJar 时使用的常用方法。因此,我创建了一个不扩展应用程序的新主类,并在插件中引用它而不是“真正的”主类。

我已经查阅了以下链接: https://www.reddit.com/r/javahelp/comments/ad1us7 /creating_a_standalone_jar_of_a_program_using/ 缺少 Maven Shade JavaFX 运行时组件 从 maven 使用 JavaFX11 构建可执行 JAR https://github.com/jesuino/fat-jar- javafx/blob/master/pom.xml

所有这些都建议我上面描述的解决方案,但创建的 jar 文件仍然不会打开我的项目,而是在打开时不执行任何操作。

这是我的 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>

这是我新创建的主类: 封装代码;

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

这是原来的:

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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文