Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch JavaFX application configured with Maven from Eclipse

I have a JavaFX application that properly runs with Maven:

mvn compile
mvn exec:java  # Launches the GUI

This is on Ubuntu, using openjdk-11-jdk, maven and openjfx Ubuntu packages.

I want to compile and run this application from the Eclipse IDE (eclipse installed with sudo snap install --classic eclipse). I have the m2e (Maven to Eclipse) plugin installed, and imported the project with File -> Import -> Maven -> Existing Maven Project. For non-JavaFX projects, the m2e plugin does everything needed to configure the project in Eclipse from Maven's pom.xml. Unfortunately, in my case, something is missing: typechecking works properly and finds the javafx.* classes, but when I try to run the application, I get the following error message in the console:

Error: JavaFX runtime components are missing, and are required to run this application

A workaround is to run the application as a Maven application (Run -> Run As -> Maven Build -> target=exec:java), but I find it less convenient and slower, so I'm looking for a way to get the application to run directly as a Java application in Eclipse.

I found the way to configure Eclipse manually (posted below as an answer), but I'm still wondering whether there's a better way, that would let Maven + m2e do the job completely, i.e. as much as possible configure everything from pom.xml and have everything "just work" in Eclipse.

The problem can be reproduced on a minimalist example, with this 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>jfxpl</groupId>
  <artifactId>jfxpl</artifactId>
  <version>0.0.1-SNAPSHOT</version>

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

  <build>
    <plugins>
      <plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.2</version>
    <configuration>
      <mainClass>App</mainClass>
    </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>
      <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <configuration>
      <mainClass>App</mainClass>
    </configuration>
      </plugin>
    </plugins>
  </build>
</project>

And any application using JavaFX like:

import javafx.application.Application;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        System.out.println("Start!"); // Or real JavaFX stuff here obviously
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
like image 235
Matthieu Moy Avatar asked Jul 23 '19 08:07

Matthieu Moy


People also ask

How do I open JavaFX project in Eclipse?

From the Eclipse IDE Main menu, select File, choose New, and then Other, as shown in Figure 2-1. From the New dialog box, expand the JavaFX folder, and select the JavaFX Project wizard, as shown in Figure 2-2. Enter the project name, specify the JRE to use, and select the Project layout option.

How do I run a Maven project in JavaFX?

Open the Maven Projects window ( View -> Tool Windows -> Maven ) and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.


1 Answers

Since you have a Maven project, you could simply use the goals from the maven plugin you are using, and get Eclipse (via m2e) to run this for you, but you have to specify which are these goals, and which configuration you want to run.

Let's say you have this HelloFX sample.

Your pom will look like:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>12</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>org.openjfx.hellofx.App</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Note that I'm using the javafx-maven-plugin instead of the exec-maven-plugin, but you could use it as well. The former properly takes care of adding the JavaFX modules to the modular path, while the latter doesn't use modules at all.

If you were on a terminal, setting JDK 12 and running:

mvn clean javafx:run

will work as expected.

However, you want to run this from Eclipse, and not from a terminal, so you can create a new Maven Build configuration from Run -> Run Configurations... -> Maven Build:

maven build

Add clean javafx:run, and make sure the JRE is properly set (JDK 12 for instance). Press apply and close the dialog.

Now you can click the Run as green icon from the toolbar or from the context menu. A dialog will show up, and you could simply select the Maven Build option:

and press OK, it will run your goals as expected.

If you didn't have any Run configuration created, when selecting Maven Build will ask you to create one and provide the required goals. This means that Eclipse doesn't guess which of the possible goals from your pom you want to run when you click the run button.

Note that alternatively, you can press the drop down icon, and open a dialog that will show your custom configurations:

Run hellofx configuration

Pressing hellofx will run the specified goals.

Finally, you can still run your project as Java Application, without the benefits of a build tool like Maven, and you will have to add the VM arguments (which means you need to download the JavaFX SDK in the first place), as in your answer. Then you could run this other configuration (hellofx2 in my case) from the same drop down button.

Note that all of this is documented in detail here.

like image 179
José Pereda Avatar answered Oct 26 '22 16:10

José Pereda