Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use JavaFX 11 in Eclipse?

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?

like image 917
Alex Chashin Avatar asked Aug 25 '18 02:08

Alex Chashin


People also ask

Why is JavaFX not working in Eclipse?

The project doesn't support the JavaFX syntax. We need to export the JavaFX jar files to the project in order to run the JavaFX application. Just Right click on the project and select properties from the options. Go to Java Build Path → Libraries.

Does Java 11 support JavaFX?

In Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application you will need to specifically include it.


1 Answers

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.

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/

like image 107
ItachiUchiha Avatar answered Sep 30 '22 17:09

ItachiUchiha