I have a multi module Maven project, and one of the modules uses JavaFX. While everything compiles fine in Eclipse, doing a
mvn clean compile
returns this error
package javafx.scene.robot does not exist
It seems like maven cannot see the javafx.graphics
module that contains the javafx.scene.robot
(but Eclipse does see it, because it gives no compile errors). How can I get Maven to compile this code?
I have created a mini version of my application that reproduces this. These are the files:
Main pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.eddy</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!-- JDK Version -->
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>mod1</module>
</modules>
</project>
Module pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.eddy</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>mod1</artifactId>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
</project>
Launch.java
package nl.eddy.mod1;
import javafx.application.Application;
import javafx.scene.robot.Robot;
import javafx.stage.Stage;
public class Launch extends Application{
public static void main(String[] args) {
}
@Override
public void start(Stage primaryStage) throws Exception {
new Robot();
}
}
module-info.java
module nl.eddy.mod1 {
requires javafx.controls;
// requires javafx.graphics; uncommenting this does not change maven's message
}
Output of mvn clean compile
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/egr/eclipse-workspace/demo/mod1/src/main/java/nl/eddy/mod1/Launch.java:[4,26] package javafx.scene.robot does not exist
[ERROR] /C:/Users/egr/eclipse-workspace/demo/mod1/src/main/java/nl/eddy/mod1/Launch.java:[15,21] cannot find symbol
symbol: class Robot
location: class nl.eddy.mod1.Launch
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for demo 0.0.1-SNAPSHOT:
[INFO]
[INFO] demo ............................................... SUCCESS [ 0.368 s]
[INFO] mod1 ............................................... FAILURE [ 2.541 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.161 s
[INFO] Finished at: 2020-02-11T13:08:56+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project mod1: Compilation failure: Compilation failure:
[ERROR] /C:/Users/egr/eclipse-workspace/demo/mod1/src/main/java/nl/eddy/mod1/Launch.java:[4,26] package javafx.scene.robot does not exist
[ERROR] /C:/Users/egr/eclipse-workspace/demo/mod1/src/main/java/nl/eddy/mod1/Launch.java:[15,21] cannot find symbol
[ERROR] symbol: class Robot
[ERROR] location: class nl.eddy.mod1.Launch
[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.
Details of compiler from log of mvn -X compile
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
Maven home: C:\apps\apache-maven-3.6.0\bin\..
Java version: 12.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-12.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows server 2012 r2", version: "6.3", arch: "amd64", family: "windows"
Compiling went fine as soon as I changed the maven-compiler-plugin release element to value 12 instead of 11. I.e., the same value as the compiler actually running Maven. Many thanks to @JosePereda, see comments.