简体   繁体   中英

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, ie 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);
    }
}

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

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:

运行hellofx配置

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 .

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

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 :

虚拟机参数配置

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

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.

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