Maven JavaFx项目可以编译,但从控制台运行时会给出"Missing JavaFX application class"(缺少JavaFX应用程序类).错误味精 [英] Maven JavaFx project compiles but running from console give "Missing JavaFX application class" error msg

查看:207
本文介绍了Maven JavaFx项目可以编译,但从控制台运行时会给出"Missing JavaFX application class"(缺少JavaFX应用程序类).错误味精的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Maven JavaFX应用程序从Java 8迁移到Java11.我已将pom.xml中的插件更新为最新的(兼容Java 11的)插件. 编译运行良好,在"target"文件夹下的正确目录中提供了jar以及所有依赖项和模块,但是当我尝试运行jar文件时,出现了可怕的"Missing JavaFX application class"错误.无论我如何尝试更改插件配置-我总是收到此错误消息,并且该应用程序将无法运行.

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. 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. No matter how I try to change the plugin configuration - I always get this error msg and the app won't run.

现在,更多发现: 1.主类DOES驻留在类和jar中的正确文件夹中. 2. Manifest文件在正确的位置,并且包含main类属性(在Java 8下可以正常工作).

Now, more findings : 1. The main class DOES reside in the right folder under classes and in the jar. 2. The Manifest file is in the right place and contains the main class attribute (which worked fine under Java 8).

这是

<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>

我通过包含JavaFX模块来运行jar,如文档所述:

I'm running the jar by including the JavaFX modules as described in the documentation :

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

让我沮丧的是,我尝试了无尽的配置,包括使用JavaFx Java 11示例中的配置.什么都行不通.

In my frustration Iv'e tried endless configurations, including using the configuration from the JavaFx Java 11 samples. Nothing works.

有什么想法吗?

推荐答案

我在此解决方法包括创建一个新的常规(非Java Fx)类,该类将成为Jar的主要类,并且该类将启动基于JavaFx的原始应用程序类.

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.

这是我找到解决方法的链接中的新类:

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.

事实证明,此处的JavaFX文档中描述了解决方法: https://openjfx .io/openjfx-docs/#modular

EDIT : it turns out that the workaround described in the JavaFX documentation here : https://openjfx.io/openjfx-docs/#modular

如此处所述,为了创建一个具有所有 必需的JavaFX依赖项,您将需要使用启动器类 这不是从应用程序扩展的.

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.

这篇关于Maven JavaFx项目可以编译,但从控制台运行时会给出"Missing JavaFX application class"(缺少JavaFX应用程序类).错误味精的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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