且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Maven使用JavaFX11构建可执行的JAR

更新时间:2021-09-28 01:18:13

我也在创建可执行jar的过程中苦苦挣扎,但是这种解决方法对我来说很有效,我希望它也对您有用:

I struggled through the process of creating an executable jar as well, but this workaround is what worked for me, and I hope it works for you as well:

首先,我没有使用jar插件,而是在pom.xml中使用了shade插件,该插件创建了一个胖罐"或超罐",其中包含您的类以及其中的所有依赖项罐子里这样,您的jar将包含在所有必需的javafx软件包和类中.也就是说,如果您将这些添加到<dependencies>部分:

First of all, instead of using the jar plugin, I used the shade plugin in pom.xml, which creates a "fat jar" or "uber jar" that contains your classes and all of the dependencies within the jar. This way, your jar will be included with all the necessary javafx packages and classes. That is, if you include these in the <dependencies> section:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>11</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
</dependency>

...或您需要的其他任何javafx库,以便应用程序运行.

... or whatever else of the javafx libraries you need in order for your application to run.

但是,仅这样做是行不通的.我假设您的主要课程Entry extends Application?

Simply doing this does not work, however. I'm assuming that your main class Entry extends Application?

我猜想jar需要了解不会扩展Application的实际Main类,因此我刚刚创建了另一个名为SuperMain的Main类( (只是一个临时名称)调用了我原来的主类,即Main:

I'm guessing the jar needs to know the actual Main class that does not extend Application, so I just created another Main class called SuperMain (it was only a temporary name) that calls my original main class, which is Main:

// package <your.package.name.here>

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

而您的情况是Entry.

所以在我的pom.xml中,我有一个名为shade的插件,如下所示:

So in my pom.xml, I have a plugin called shade that looks like this:

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>my.package.name.SuperMain</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

在执行mvn package之后,应该有一个阴影"的jar.

and there should be a jar that's "shaded" after you execute mvn package.

感谢这篇文章的答案: JavaFX 11 :使用Gradle创建一个jar文件

Thanks to the answer to this post: JavaFX 11 : Create a jar file with Gradle

希望这会有所帮助!