Javafx 项目不能用 maven 编译

Posted

技术标签:

【中文标题】Javafx 项目不能用 maven 编译【英文标题】:Javafx project doesn't compile with maven 【发布时间】:2013-10-01 16:23:56 【问题描述】:

我有一个 Java FX 项目,并且正在使用 maven 3.0.5 进行构建。当我运行 mvn package/install 时,项目失败并抱怨包 javafx.* 不存在。据我了解,Javafx 被打包在 Java 1.7 中,因此构建不支持它是没有意义的。当我在我的 IDE (intellij) 中编译它时,它工作正常,之后我的 maven 构建也成功安装,但是我不希望每次进行全新安装时都必须通过 IDE。有什么想法吗?

这是我的 pom:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>thenaglecode</artifactId>
        <groupId>com.thenaglecode</groupId>
        <version>1.0.0.Pre-Alpha</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>system-setup</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <includeEmptyDirs>true</includeEmptyDirs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>gui</headerType>
                            <jre>
                                <path>C:\Program Files\Java\jre7</path>
                                <minVersion>1.7.0</minVersion>
                                <initialHeapSize>128</initialHeapSize>
                                <maxHeapSize>1024</maxHeapSize>
                            </jre>
                            <jar>$project.build.directory/$project.build.finalName.jar</jar>
                            <outfile>$project.build.directory/systemsetup.exe</outfile>
                            <classPath>
                                <mainClass>com.thenaglecode.Start</mainClass>
                            </classPath>
                            <singleInstance>
                                <mutexName>thenaglecode-system-setup</mutexName>
                            </singleInstance>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这是我的日志:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/CommandHelper.java:[3,26] package javafx.application does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ListAndOptionsController.java:[3,26] package javafx.beans.value does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ListAndOptionsController.java:[4,26] package javafx.beans.value does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ListAndOptionsController.java:[5,26] package javafx.collections does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ListAndOptionsController.java:[6,20] package javafx.event does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ListAndOptionsController.java:[7,20] package javafx.event does not exist
...
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/Context.java:[3,28] package javafx.scene.control does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/ServiceMuncher.java:[3,31] package com.sun.istack.internal does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/TypeInfo.java:[3,31] package com.sun.istack.internal does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/NumericOnlyTextFieldChangeListener.java:[3,31] package com.sun.istack.internal does not exist
[ERROR] /C:/Users/Macindows/IdeaProjects/thenaglecode/system-setup/src/main/java/com/thenaglecode/NumericOnlyTextFieldChangeListener.java:[4,26] package javafx.beans.value does not exist
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Process finished with exit code 1

感谢您的帮助。

【问题讨论】:

这很烦人:***.com/questions/9294646/… - 适用于 JavaFX 8... Maven project with JavaFX (with jar file in `lib`)的可能重复 【参考方案1】:

将它放在依赖项中(取决于您的 jdk 所在的位置)将有助于依赖项编译,但是您可能应该将其替换为用户可以在其 settings.xml 或 maven 系统属性中指定的变量:

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javaFX</artifactId>
        <version>2.2</version>
        <scope>system</scope>
        <systemPath>C:\Program Files\Java\jre7\lib\jfxrt.jar</systemPath>
    </dependency>

查看Maven project with JavaFX (with jar file in `lib`)了解更多详情

【讨论】:

【参考方案2】:

如果您在使用 Maven 的 JavaFx 版本 17 的模块化项目中遇到这些错误,只需尝试通常的操作,例如站在头上、在天花板上行走、向相反方向旋转地球,甚至阅读 https://openjfx.io/openjfx-docs/ 然后只需切换到 17.0.1 或更高版本,它应该可以工作:

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

【讨论】:

将版本从 17 更改为 17.0.1 工作形式【参考方案3】:

这个解决方案对我有用。

将 sample.fxml 移动到资源文件夹。 这需要将您的代码更改为 getResource("/sample.fxml")。

原答案:Problem with JavaFX and maven

【讨论】:

以上是关于Javafx 项目不能用 maven 编译的主要内容,如果未能解决你的问题,请参考以下文章

无法用maven编译java项目

创建基于maven的javaFX项目

如何在 IntelliJ IDEA 中创建 JavaFX Maven 项目?

带有 JavaFX 的 Maven 项目(在 `lib` 中有 jar 文件)

如何使用 maven 部署 JavaFX 项目,包括自定义和非模块化依赖项?

JavaFX 不存在使用 Java 9 和 Intellij Idea