13

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);
    }
}
Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
  • See https://openjfx.io/openjfx-docs/#IDE-Eclipse, Maven sections – José Pereda Jul 23 '19 at 08:04
  • Thanks, that does help, but doesn't fully answer the point of using Eclipse + Maven (and the associated frustration of not having m2e configure everything automatically). – Matthieu Moy Jul 23 '19 at 08:13
  • Have you tried [e(fx)clipse](https://projects.eclipse.org/projects/technology.efxclipse) plugin? I used it with Java 8 and it provided good support for creating new JavaFX projects, running JavaFX applications, FXML support, CSS support, etc. – vl4d1m1r4 Jul 26 '19 at 10:11
  • Show your `pom.xml`. Is [this the same issue](https://stackoverflow.com/a/55920911/6505250)? See also [this m2e mailing list post](https://www.eclipse.org/lists/m2e-users/msg05705.html). – howlger Jul 26 '19 at 13:15
  • 1
    JavaFX is unsupported past java 8 – Joe Jul 30 '19 at 01:10
  • @howlger: I added my pom.xml in the post. It's not the same issue as https://stackoverflow.com/a/55920911/6505250 and the m2e mailing list post, since they deal with compilerArgs, and what I need here is a runtime flag, but it's similar so your comment does help, thanks. – Matthieu Moy Jul 31 '19 at 13:35
  • @Joe: JavaFX is not shipped with Java anymore, but it's still supported. – Matthieu Moy Jul 31 '19 at 13:39

4 Answers4

5

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.

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • This is what I meant by "A workaround is to run the application as a Maven application", but your answer documents it in details, which should be helpful for other readers. Thanks! – Matthieu Moy Jul 31 '19 at 13:44
  • So if I get it right, you say that using Maven to run from Eclipse your Maven project is a workaround? As a Java Application, Eclipse will need to know the VM arguments, as JavaFX requires to be in the module-path. As Maven project, you are using plugins that take care of that for you (i.e. javafx-maven-plugin or exec-maven-plugin), so all you need to do is tell Eclipse what plugins and goals you want to run). – José Pereda Jul 31 '19 at 14:33
1

OpenJDK for linux doesnt come with the JAVAFX framework. you need to download it and install.

sudo apt-get install openjfx

After installing use following steps:

Set the SDK first

Step 1:

GOTO File -> Project Structure -> Modules -> Dependency >> + [on left-side of window] click + sign

Step 2 :

Run >> Edit Configurations

Step3: Add below parameter in VM config :

--module-path /path/to/JavaFX/lib --add-modules=javafx.controls
Akash
  • 587
  • 5
  • 12
  • Thanks, but I don't think this answers the question any better than the answer I provided myself while posting the question (https://stackoverflow.com/questions/57159440/launch-javafx-application-configured-with-maven-from-eclipse/57159615#57159615) – Matthieu Moy Jul 31 '19 at 13:42
0

One solution is to configure Eclipse manually to add the required modules: Run -> Run configurations -> Java Application -> Arguments -> VM Arguments, add --module-path /path/to/JavaFX/lib --add-modules=javafx.controls:

VM Arguments configuration

If you need other modules, specify then as a comma-separated list like --add-modules javafx.controls,javafx.fxml.

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
0

This might be duplicate, did you try this

<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
    <mainClass>your.main.class.which.extends.javafx.Application</mainClass>
</configuration>
</plugin>

Provided here: JavaFX Application with Maven in Eclipse

Also, there are docs for it, which probably correlate exactly to Java 11.

https://openjfx.io/openjfx-docs/

Here are the OpenJFX releases, provided from the docs.

https://gluonhq.com/products/javafx/

Also,

https://openjfx.io/openjfx-docs/images/ide/eclipse/modular/maven/eclipse05.png

Is a great image of the build through maven.

Joe
  • 1,316
  • 9
  • 17