繁体   English   中英

Maven 编译错误:包 javafx 不存在(使用 javafx-maven-plugin)

[英]Maven Compilation Error: package javafx does not exist (with javafx-maven-plugin)

我正在做一个类项目,需要使用 Maven 作为 JavaFx 项目的构建工具。

这是我的 pom.xml 设置,我使用了 2 个插件。 根据我的理解: maven-compiler-plugin是强制执行 java jdk 1.8 作为编译器,而javafx-maven-plugin是自动下载 javafx 库。

此外,我的主启动器 Main.java 文件位于src/main/java/Main/Main.java pom.xmlsrc文件夹处于同一级别。

<build>
    <plugins>
        <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.5.1</version>
              <configuration>
                  <fork>true</fork>
                  <compilerVersion>1.8</compilerVersion>
                  <verbose>true</verbose>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
          <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>Main.Main</mainClass>
            </configuration>
        </plugin>

    </plugins>
</build>

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

并且我在pom.xml同目录下执行mvn package命令,看来maven-compiler-plugin 3.5.1已经下载好了。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building PhotoshopUltraLight 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.5.1/maven-compiler-plugin-3.5.1.pom
Downloaded: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.5.1/maven-compiler-plugin-3.5.1.pom (10 KB at 23.3 KB/sec)
Downloading: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/28/maven-plugins-28.pom
Downloaded: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/28/maven-plugins-28.pom (12 KB at 170.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/27/maven-parent-27.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/27/maven-parent-27.pom (40 KB at 446.7 KB/sec)
Downloading: 
https://repo.maven.apache.org/maven2/org/apache/apache/17/apache-17.pom
Downloaded: 
https://repo.maven.apache.org/maven2/org/apache/apache/17/apache-17.pom (16 KB at 261.3 KB/sec)
Downloading: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.5.1/maven-compiler-plugin-3.5.1.jar
Downloaded: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.5.1/maven-compiler-plugin-3.5.1.jar (50 KB at 778.5 KB/sec)

但是,编译总是失败。

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/ImageLayer.java:[3,25] error: package javafx.scene.image does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/ImageLayer.java:[4,25] error: package javafx.scene.image does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/ImageLayer.java:[5,26] error: package javafx.scene.layout does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[4,25] error: package javafx.beans.value does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[5,25] error: package javafx.beans.value does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[6,22] error: package javafx.geometry does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[7,27] error: package javafx.scene.control does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[8,27] error: package javafx.scene.control does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[9,26] error: package javafx.scene.layout does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[10,26] error: package javafx.scene.layout does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[11,25] error: package javafx.scene.paint does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/Layer.java:[12,25] error: package javafx.scene.shape does not exist
[ERROR] /mnt/c/Users/meifi/Documents/Photoshop-Ultra-Light/src/main/java/Layers/ImageLayer.java:[11,4] error: cannot find symbol
[ERROR]   symbol:   class ImageView
location: class ImageLayer

我不知道哪一步是错误的......任何想法都会非常感激。

Oracle JDK 8 与 JavaFX 捆绑在一起,但未与 OpenJDK 8 捆绑在一起

JavaFX 也从 Oracle JDK/JRE 11 中删除。我在 JDK 11 中遇到了同样的错误。通过在 pom.xml 中包含它来解决它:

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

参考:

  1. https://openjfx.io/openjfx-docs/#maven
  2. https://www.infoworld.com/article/3305073/removed-from-jdk-11-javafx-11-arrives-as-a-standalone-module.html

如果在使用 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>

暂无
暂无

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

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