缺少Maven Shade JavaFX运行时组件 [英] Maven Shade JavaFX runtime components are missing

查看:295
本文介绍了缺少Maven Shade JavaFX运行时组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用maven依赖项创建一个JFX11自包含jar。从我所做的研究来看,似乎最好的方法是通过maven shade插件。但是,当我运行它时,我收到此错误:

I'm trying to create a JFX11 self-containing jar using maven dependencies. From the research I've done, it seems the best way to do this is through the maven shade plugin. However, When I run it, I get the this error:

错误:缺少JavaFX运行时组件,并且需要运行此应用程序

Error: JavaFX runtime components are missing, and are required to run this application

我不明白为什么会这样。我搞砸了什么?有一个更好的方法吗?我也尝试过使用相同消息的maven程序集插件。

I don't understand why this is happening. What am I messing up? Is there a better way to do this? I've also tried the maven assembly plugin with the same message.

pom文件供参考

<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>Application</groupId>
    <artifactId>Main</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpaceRunner</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>10</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>Application.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                Application.Main
                            </mainClass>
                        </manifest>
                    </archive>
                </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>Application.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


推荐答案

答案解释了为什么胖子/超级jar在JavaFX 11上失败。简而言之:

This answer explains why a fat/uber jar fails on JavaFX 11. In short:


此错误来自java.base模块中的sun.launcher.LauncherHelper。原因是Main应用程序扩展了Application并有一个主要方法。如果是这种情况,LauncherHelper将检查 javafx.graphics 模块是否作为命名模块出现。如果该模块不存在,则启动中止。

This error comes from sun.launcher.LauncherHelper in the java.base module. The reason for this is that the Main app extends Application and has a main method. If that is the case, the LauncherHelper will check for the javafx.graphics module to be present as a named module. If that module is not present, the launch is aborted.

并且已经为Gradle建议修复。

And already proposes a fix for Gradle.

对于Maven,解决方案完全相同:提供一个不从 Application 扩展的新主类。

For Maven the solution is exactly the same: provide a new main class that doesn't extend from Application.

您的应用程序包中包含新类(错误名称):

You will have new class in your application package (bad name):

// NewMain.java
public class NewMain {

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

您现有的 Main class,原样:

And your existing Main class, as is:

//Main.java
public class Main extends Application {

    @Override
    public void start(Stage stage) {
        ...
    }

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

现在你需要修改你的pom并设置你的主不同插件的类:

Now you need to modify your pom and set your main class for the different plugins:

<mainClass>application.NewMain</mainClass>

特定于平台的胖罐

最后,使用shade插件,你将在你的机器上生成一个胖罐

Finally, with the shade plugin you are going to produce a fat jar, on your machine.

这意味着到目前为止,您的JavaFX依赖项正在使用唯一的分类器。例如,如果你在Windows上,Maven将在内部使用 win 分类器。这具有仅包括Windows的本机库的效果。

This means that, so far, your JavaFX dependencies are using a unique classifier. If for instance you are on Windows, Maven will be using internally the win classifier. This has the effect of including only the native libraries for Windows.

所以您正在使用:


  • org.openjfx:javafx-controls:11

  • org.openjfx:javafx-controls:11:win

  • org.openjfx :javafx-graphics:11

  • org.openjfx:javafx-graphics:11:win< - 这包含Windows的原生dll

  • org.openjfx:javafx-base:11

  • org.openjfx:javafx-base:11:win

  • org.openjfx:javafx-controls:11
  • org.openjfx:javafx-controls:11:win
  • org.openjfx:javafx-graphics:11
  • org.openjfx:javafx-graphics:11:win <-- this contains the native dlls for Windows
  • org.openjfx:javafx-base:11
  • org.openjfx:javafx-base:11:win

现在,如果您生成fat jar,您将捆绑所有这些依赖项(以及项目中的其他常规第三方依赖项),并且您将能够以下列方式运行项目:

Now, if you produce the fat jar, you will bundle all those dependencies (and those other regular third party dependencies from your project), and you will be able to run your project as:

java -jar myFatJar-1.0-SNAPSHOT.jar

虽然这非常好,但是如果你想发布你的jar,请注意这个jar是而不是跨平台,它只能在你的平台上运行,在这种情况下Windows。

While this is very nice, if you want to distribute you jar, be aware that this jar is not cross-platform, and it will work only on your platform, in this case Windows.

跨平台Fat Jar

有一个解决方案可以生成一个可以分发的跨平台jar:包括其他平台的其他本机库。

There is a solution to generate a cross-platform jar that you can distribute: include the rest of the native libraries of the other platforms.

这很容易完成,因为您只需要包含三个平台的图形模块依赖关系:

This can be easily done, as you just need to include the graphics module dependencies for the three platforms:

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
        <classifier>win</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
        <classifier>linux</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
        <classifier>mac</classifier>
    </dependency>
</dependencies>

尺寸

这种方法存在一个主要问题:尺寸。正如您在其他答案中所看到的,如果您使用WebView控件,您将捆绑大约220 MB,因为WebKit本地库。

There is a main issue with this approach: the size. As you can see in this other answer, if you use the WebView control, you will be bundling around 220 MB due to the WebKit native libraries.

这篇关于缺少Maven Shade JavaFX运行时组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆