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.
Install Eclipse 2018-09 from here.
Install JDK 11 from here.
Add Java 11 as an installed JRE to Eclipse: Eclipse -> Window -> Preferences -> Java -> Installed JREs -> Add.
Download JavaFX 11 ea from here.
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.
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.
Add a package
javafx11
and the main application classHelloFX
:
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.
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
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.
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
Handling Passwords Used for Auth in Source Code
What Is the Solution for the N+1 Issue in JPA and Hibernate
How to Inject a Property Value into a Spring Bean Which Was Configured Using Annotations
Call "Java -Jar Myfile.Jar" with Additional Classpath Option
Do Spurious Wakeups in Java Actually Happen
Determine File Creation Date in Java
Polymorphism: Why Use "List List = New Arraylist" Instead of "Arraylist List = New Arraylist"
Method for Evaluating Math Expressions in Java
Programmatically Getting the MAC of an Android Device
Memory Address of Variables in Java
Java Socket Why Server Can Not Reply Client
Differencebetween Method Overloading and Overriding
Handling Exceptions from Java Executorservice Tasks
How to Attach Source in Eclipse
Initial Size for the Arraylist
How to Use Cardlayout with Netbeans Gui Builder
How to Access a Value Defined in the Application.Properties File in Spring Boot