简体   繁体   中英

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

I am doing a class project, requiring using Maven as build tool for a JavaFx project.

This is my pom.xml setting, where I use 2 plugins. Per my understanding: maven-compiler-plugin is to enforce java jdk 1.8 as compiler, and javafx-maven-plugin to auto download javafx library.

Also, my main launcher Main.java file is located at src/main/java/Main/Main.java . pom.xml is at the same level as src folder.

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

And I execute mvn package command under the same directory of pom.xml, and it seems the maven-compiler-plugin 3.5.1 was downloaded fine.

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

However, compilation always failed.

[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

I have no clue which step was wrong... Any thought would be very appreciated.

Oracle JDK 8 is bundled with JavaFX but not the OpenJDK 8

JavaFX is also removed from Oracle JDK/JRE 11. I was getting the same error with JDK 11. Solved it by including this in pom.xml:

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

Ref:

  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

If this happens to you in a modular project with JavaFx version 17 using Maven, just try the usual things like standing on your head, walking on the ceiling, rotating the Earth in the opposite direction or even reading https://openjfx.io/openjfx-docs/ and then just switch to the version 17.0.1 or later and it should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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