Issue
I'm trying to figure out what would be the minimum pom.xml
to use for creating a hello-world javafx application that can be provided to the client as a jar
+ ./lib
and run with java -jar
.
I've used:
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.javafx</groupId>
<artifactId>javafx-jdk14-example</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.javafx.app.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics </artifactId>
<version>14</version>
<classifier>linux</classifier>
</dependency>
</dependencies>
</project>
Main.java
package com.example.javafx.app;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER); // Override default
grid.setHgap(10);
grid.setVgap(12);
Button btn = new Button();
btn.setText("Clickme");
btn.setOnAction((t) -> System.out.println("Clicked!"));
grid.add(btn, 0, 0);
primaryStage.setScene(new Scene(grid, 640, 480));
primaryStage.show();
}
}
Note that I don't use a module-info.java
on my app.
But when I run it:
mvn clean package
cd target
/jdk14/bin/java -jar javafx-jdk14-example-1.0.0.jar
I get:
Error: JavaFX runtime components are missing, and are required to run this application
If I use the same with JDK10 (also compiler.source/target=10
and comment-out org.openjfx
deps) then the above works fine.
Is there any other way (on JDK11+) to build and then be able to run the application from inside target directory without requiring to use javafx-maven-plugin
?
Solution
With the help of @Slaw I found the way to run an the javafx application (created without javafx-maven-plugin
):
cd target
java -p lib/ --add-modules javafx.controls -jar javafx-jdk14-example-1.0.0.jar
I need to explicitly add the javafx.controls
module.
Bottom-line:
javafx-maven-plugin
is not required for building and shipping a javafx application.
Answered By - Marinos An
Answer Checked By - Pedro (JavaFixing Volunteer)