How do I use JavaFX 11 in Eclipse?

Alex Chashin picture Alex Chashin · Aug 25, 2018 · Viewed 26.9k times · Source

I have some trouble with JavaFX. I wanted to start creating apps, desktop or mobile, at least something. So I found out I could use the JavaFX library for it. But as far as I understood, it was excluded from JDK 9. I'm actually using OpenJDK 11 on Ubuntu 18 (though Eclipse writes I have the JavaSE 10 environment, that is where I'm also a bit confused) and 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 the included JavaFX, but anyway, how can I use JavaFX in such conditions in Eclipse?

Answer

ItachiUchiha picture ItachiUchiha · Aug 25, 2018

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:

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.

enter image description here

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/