How to add JavaFX runtime to Eclipse in Java 11?
I had problems regarding FXMLLoader
... It was not able to find the class.
I resolved this by changing the arguments showed above, to load all modules, not only javafx.controls
:
--module-path <path to the javafx sdk> --add-modules=ALL-MODULE-PATH
I'd like to share my findings regarding launching a javafx 11 app. I'm talking about an existing legacy javafx app originally developed without modules (e.g. w/ Java 8).
The most interesting way IMO is Method #2b below, which requires the minimum of changes, i.e. it doesn't need -module-path
argument. I mention that I'm using maven, so cf. previous answers the first step is to add a dependency in your pom.xml
towards javafx. E.g.:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
In order to launch, I found several options:
1a) Launch using maven from command line
Configure in your pom.xml
a section like:
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.openjfx.hellofx.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
After this, from a console window you can run mvn exec:java
, and this should launch the application. Maven will take care of adding all the entries from classpath to module path. I remind the CTRL + ALT + T
shortcut in Eclipse, that opens a terminal window directly in the IDE.
1b) Launch using maven, but as an Eclipse launch config
Right click on the project > Run As > Maven Build.... Then enter exec:java
in the Goals text box. Advantage over the previous method: a bit more integrated to Eclipse. And easy debugging. You only need to relaunch the launch config in Debug mode and that's it. May I remind that the Eclipse launch configs can be stored as .launch
files directly in the project dir (repo), thus shareable/reusable by colleagues. Use the last tab, Common, in the Edit Configuration window.
2a) Launch using Eclipse and specifying -module-path
using a variable
Right click on the main class > Run As > Java Application. The first time you click a new launch configuration is created; probably it won't work, so you need to edit it (e.g. **Run button (w/ Play icon) in the toolbar > Run configurations > select the one corresponding to your class. Or CTRL + click
on it after clicking on Play).
Then add this in Arguments > VM arguments:
--module-path ${project_classpath:REPLACE_ME_WITH_YOUR_PROJECT_NAME} --add-modules javafx.controls,javafx.fxml
So the trick here was to use the ${project_classpath}
variable. Because otherwise you should have needed to write exactly the path towards the javafx jar, that are somewhere in your .m2
repo. Doing this would make the launch config not easily reusable by colleagues.
2b) Launch using Eclipse WITHOUT specifying -module-path
This is la piece de résistence, which I found by mistake, after about 5h of Java 11 & modules "fun". Your application can work out of the box, without touching -module-path
. The javafx libs will of course still need to be in your classpath (but this is handled by mvn). The trick is (cf. this) that your main app SHOULD NOT extend Application
(e.g. MyApplication
below). If this is your case, then make a new class with a main()
function that does e.g.:
Application.launch(MyApplication.class);
NOTE
In my case (i.e. on Windows, having a default JDK 8 in the system), there is a known issue regarding launching javafx. The solution is to add the following VM arg: -Djava.library.path=C:
. So actually for all 4 methods described above I needed to add this.
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.