简体   繁体   English

Maven JavaFx项目已编译,但从控制台运行却出现“缺少JavaFX应用程序类”错误,消息为msg

[英]Maven JavaFx project compiles but running from console give “Missing JavaFX application class” error msg

I'm working migrating my Maven JavaFX app from Java 8 to Java 11. Iv'e updated the plugins in my pom.xml to the most current (Java 11 compliant) plugins. 我正在将Maven JavaFX应用程序从Java 8迁移到Java11。我已将pom.xml中的插件更新为最新的(兼容Java 11的)插件。 Compilation runs fine, giving me the jars and all dependencies and modules in the right directories under the "target" folder but when I try to run my jar file I get the dreaded "Missing JavaFX application class " error. 编译运行良好,在“目标”文件夹下的正确目录中提供了jar以及所有依赖项和模块,但是当我尝试运行jar文件时,出现了可怕的“缺少JavaFX应用程序类”错误。 No matter how I try to change the plugin configuration - I always get this error msg and the app won't run. 无论我如何尝试更改插件配置-我总是收到此错误消息,并且该应用程序将无法运行。

Now, more findings : 1. The main class DOES reside in the right folder under classes and in the jar. 现在,有更多发现:1.主类确实位于类和jar中的正确文件夹中。 2. The Manifest file is in the right place and contains the main class attribute (which worked fine under Java 8). 2. Manifest文件在正确的位置,并且包含main class属性(在Java 8下可以正常工作)。

Here is the relevant part of the 这是相关的部分

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <source>11</source>
            <release>11</release>
            <showWarnings>true</showWarnings>
            <showDeprecation>true</showDeprecation>
            <compilerVersion>11</compilerVersion>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.ow2.asm</groupId>
                <artifactId>asm</artifactId>
                <version>7.0</version>
            </dependency>
        </dependencies>
</plugin>
<plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                   <addClasspath>false</addClasspath>
                   <mainClass>${mainClass}</mainClass>
                </manifest>
                <manifestEntries>
                   <JavaFX-Application-Class>${mainClass}</JavaFX-Application-Class>
                </manifestEntries>
             </archive>
             <outputDirectory>${project.build.directory}/libs</outputDirectory>
        </configuration>
</plugin>
<plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
                <execution>
                        <id>copy-libs</id>
                        <phase>prepare-package</phase>
                        <goals>
                                <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                                <outputDirectory>${project.build.directory}/libs</outputDirectory>
                                <includeScope>runtime</includeScope>
                                <excludeGroupIds>org.openjfx</excludeGroupIds>
                        </configuration>
                </execution>
                <execution>
                        <id>copy-modules</id>
                        <phase>prepare-package</phase>
                        <goals>
                                <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                                <outputDirectory>${project.build.directory}/mods</outputDirectory>
                                <includeScope>runtime</includeScope>
                                <includeGroupIds>org.openjfx</includeGroupIds>
                        </configuration>
                </execution>
        </executions>

I'm running the jar by including the JavaFX modules as described in the documentation : 我通过包含JavaFX模块来运行jar,如文档中所述:

java -verbose --module-path ../mods \
    --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.swing \
     -jar jar-file-name.jar \
     package.class.MainClass

In my frustration Iv'e tried endless configurations, including using the configuration from the JavaFx Java 11 samples. 令我沮丧的是,我尝试了无尽的配置,包括使用JavaFx Java 11示例中的配置。 Nothing works. 没用。

Any ideas? 有任何想法吗?

I found a workaround at https://github.com/javafxports/openjdk-jfx/issues/236 . 我在https://github.com/javafxports/openjdk-jfx/issues/236找到了解决方法。

This workaround includes creating a new, regular (non Java Fx) class that will be the main class of the Jar and this class will launch the original JavaFx based application class. 此解决方法包括创建一个新的常规(非Java Fx)类,该类将成为Jar的主要类,并且该类将启动基于JavaFx的原始应用程序类。

This is the new class from the link where I found the workaround: 这是我找到解决方法的链接中的新类:

public class Main {

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

Hope this helps someone. 希望这对某人有帮助。 I'm not marking this answer as the correct one due to the fact that it's a workaround and not a real solution. 由于这是一种解决方法而不是真正的解决方案,因此我并未将此答案标记为正确的答案。

EDIT : it turns out that the workaround described in the JavaFX documentation here : https://openjfx.io/openjfx-docs/#modular 编辑:事实证明,此处的JavaFX文档中描述了解决方法: https//openjfx.io/openjfx-docs/#modular

As explained here, in order to create a runnable jar with all the required JavaFX dependencies, you will need to use a launcher class that doesn't extend from Application. 如此处所述,为了创建具有所有必需的JavaFX依赖关系的可运行jar,您将需要使用不从Application扩展的启动程序类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM