首页 > 解决方案 > 如何使用 JDK 11/Maven/Eclipse IDE 运行 JavaFX 应用程序

问题描述

问题:从 Eclipse IDE 运行基于 Maven 非模块项目(项目名称 =“howdyjfx”)的 JavaFX 应用程序会生成以下编译错误:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project howdyjfx: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid.

开发环境及配置:

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spindotta.jfx11.testbed</groupId>
    <artifactId>howdyjfx</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13-ea+8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13-ea+8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.2</version>

                <configuration>
                    <!-- Is this necessary? The 'Run' configuration goals are "clean exec:java" -->
                    <executable>C:\Program Files\Java\jdk-11.0.3\bin\java</executable>

                    <!-- Workaround to short-circuit jdk1.8 which is needed to run Eclipse 
                        but is toxic for jdk11 and higher -->
                    <options>
                        <option>-Djava.library.path=C:\tmp</option>
                    </options>

                    <!-- Main class - is this correct? -->
                    <mainClass>com.spindotta.jfx11.testbed.howdyjfx.HowdyJFX</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

申请代码为:

    public class HowdyJFX extends Application {

         @Override
         public void start(Stage primaryStage) throws Exception {
              final String javaVersion = System.getProperty("java.version");
              final String javafxVersion = System.getProperty("javafx.version");
              final Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
              final Scene scene = new Scene(new StackPane(l), 640, 480);
              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

上述配置和代码基于“入门”指南Sr. Jose Pereda 的 Github 帖子,以及对去年 9 月提出的类似问题的回答。但是,无论出于何种原因,我都无法使其正常工作。这更加令人沮丧,因为我正在完成一个广泛的 JFX 控件库,该库可以毫无问题地编译(使用 JDK 11 和 JavaFX 11)并且在 Scene Builder 中工作正常(除了一些与这里无关的 Scene Builder 问题)。

提前感谢您提供任何有用的建议!

标签: eclipsemavenjavafx

解决方案


文件中的配置是正确的,但是部分pom主类的规范是错误的。在“入门”指南和 Github repo 上的资料中, 的名称是,它是加号加. 因为我的项目的和分别是 和 ,所以我将它们与. 这是不正确的,因为该项目具有标准的 Maven 配置和一个顶级文件夹。在插件中指定为解决了问题。<configuration>javafx-maven-pluginmainClass‘ org.openjfx.hellofx.App<groupId><artifactId>simple class name<groupId><artifactId>com.spindotta.jfx11.testbedhowdyjfxsimple class name HowdyJFXsource/main/javahowdyjfxhowdyjfx/HowdyJFXmainClass

对于它的价值,<configuration>for the中指定的项目javafx-maven-plugin是必不可少的;省略 any 将生成一个ERROR. 应该可以在 中指定这些值,尽管我发现使用模板Run configurations -> JRE tab -> VM arguments更容易处理这些值。pom

感谢 Jose Pereda 澄清了发布时Goals必须指定的问题Run configurations

现在还有另一个问题。我需要能够调试由入门指南Github repo中建议的方法启动的 JavaFX 应用程序。我在分配变量的第一行中设置了一个Eclipse断点main class HowdyJFX,以便应用程序将在该点停止,但这并没有发生。相反,应用程序就像断点不存在一样运行。这是一个与当前不同的问题,并在其他地方提出。


推荐阅读