How to Add Javafx Runtime to Eclipse in Java 11

How to add JavaFX runtime to Eclipse in Java 11?

Following the getting started guide, these are the required steps to run JavaFX 11 from Eclipse.

  1. Install Eclipse 2018-09 from here.

  2. Install JDK 11 from here.

  3. Add Java 11 as an installed JRE to Eclipse: Eclipse -> Window -> Preferences -> Java -> Installed JREs -> Add.

  4. Download JavaFX 11 ea from here.

  5. Create a User Library: Eclipse -> Window -> Preferences -> Java -> Build Path -> User Libraries -> New. Name it JavaFX11 and include the jars under the lib folder from JavaFX 11-ea.

  6. Create a Java project. You don't need to add a module-path class. Make sure that you select Java 11 and you add the JavaFX11 library to the project's modulepath.

  7. Add a package javafx11 and the main application class HelloFX:

    package javafx11;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class HelloFX extends Application {

@Override
public void start(Stage stage) {
String version = System.getProperty("java.version");
Label l = new Label ("Hello, JavaFX 11, running on "+version);
Scene scene = new Scene (new StackPane(l), 300, 200);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch();
}

}

Note that the editor shouldn't complain about JavaFX classes, as we have included the user library.


  1. Add runtime arguments. Edit the project's run configuration, and add these VM arguments:

    --module-path C:\Users<user>\Downloads\javafx-sdk-11\lib --add-modules=javafx.controls

  2. Finally, run the project. It should work fine.

Error: JavaFX runtime components are missing - JavaFX 11 and OpenJDK 11 and Eclipse IDE

Your problem is not compiling the project, but running it.
Since your main is defined in your Application-extension, running the project will require JavaFX in your module path on startup.

So either outsource your main into a class different from your Application or add the JavaFX modules with VM arguments:

--module-path="<javafx-root>\lib" --add-modules="javafx.base,javafx.controls,..."

See this for some more info.

How do I use JavaFX 11 in Eclipse?

There are multiple points in your post which needs clarification. I will try to answer them in different bullet points:

But as far as I understood, it(JavaFX) was excluded from JDK 9.

JavaFX will be decoupled from Oracle JDK starting JDK 11. I stress on Oracle JDK because JavaFX was never a part of OpenJDK. Not even in OpenJDK 8.

I'm actually using OpenJDK 11 on Ubuntu 18 (Though eclipse writes I have JavaSE 10 environment, that is where I'm also a bit confused)

For Java 11 support in Eclipse, you need to install
Java 11 Support for Eclipse Photon plugin.

Here are a few Examples on how to run Java 11 applications in Eclipse

I installed openjfx using sudo apt install openjfx and I can't make eclipse work with JavaFX.

I'm not sure if there's any sense not to use JDK 8 with included JavaFX, but anyway, how can I use JavaFX in such conditions in eclipse?

Since OpenJDK 11 or Oracle JDK 11 will not come bundled with JavaFX, your best bet is to either download the JavaFX SDK from here or here and load them in your IDE.

If you are used to build tools, you can directly use the JavaFX runtime jars which are available in Maven Central.

For a tutorial on how to run JavaFX 11 on OpenJDK 11, you can follow:

  • Getting Started with JavaFX 11
  • JavaFX on JDK 11

JavaFX 11 and Eclipse

At the time of writing this post, you need Eclipse 4.9M3 to work with JavaFX 11.

Once you have eclipse, JDK 11 and JavaFX 11 SDK, you can either opt to create:

  • Module based project
  • Non-module based project (No module-info.java required)

Module based Project

Create a Java project and add JavaFX jars from the Java FX 11 SDK to the module path of the project.

Sample Image

Create a module.info and declare its dependency of javafx.controls module. javafx11 is the name of the package which contains your Java file.

module javafx11 {
requires javafx.controls;
exports javafx11;
}

Run the program \o/

Non-module based Project

Create a Java project and add JavaFX jars from the Java FX 11 SDK to either the module-path or classpath of the project.

Add the following JVM args to the run configuration of the project:

--module-path=path-to-javafx-skd/lib --add-modules=javafx.controls

Run the program \o/



Related Topics



Leave a reply



Submit