How to add JavaFX runtime to Eclipse in Java 11?

2020-01-23 02:53发布

问题:

I am getting the following error as Java 11 excluded the JavaFX as part of the latest version.

Error: JavaFX runtime components are missing, and are required to run
this application

So how can I add JavaFX to Eclipse in Java 11? Thanks.

回答1:

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.

8 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

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



回答2:

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.



回答3:

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


回答4:

Using Carlao2005's advice I was able to put together a complete guide for getting JavaFX to work in Eclipse now that JavaFX does not come with the Java SDK. This assumes you have Eclipse with the E(FX)clipse plug in set up and an installation of the JavaFX SDK.

Go through this step for each new Java FX project to be built. First, go to file > new > other and find the option for creating a new Java FX Project. Once the project is created you will notice it does not compile as Eclipse needs to know about the JavaFX libraries. Access the project properties and then find the Java build path. Select the Libraries tab and then module path. Then select the add library button and then select the User library option before pressing next.

On the window that appears, select new and name the library JavaFX13. Select add external jars and then find the lib folder of your JavaFX 13 installation directory and select all of the .jar folders then apply and close or finish on all windows.

If you attempt to run the project you will find it will not be able to find application.main. To fix this we have one last step. Right click main.java in Eclipse's package explorer. Select run as > run configurations and then select the arguments tab. In the vm arguments add the following (where the module path points to your installation of the Java FX sdk):

--module-path C:\Programs\Java\javafx-sdk-13.0.1\lib --add-modules=ALL-MODULE-PATH

That's it, you should now be able to run the Java FX project and see a default window. If running on Mac OS de-select the option -XstartOnFirstThread argument when launching SWT if you experience problems getting a window to display.



回答5:

@José Pereda is perfectly right, everything works as it should. I have tested it with the Eclipse release 2018-12. So, after you download the IDE (https://www.eclipse.org/downloads/packages/release/2018-12/r/eclipse-ide-java-developers) and install the e(fx)clipse plugin, you should be able to create a "JavaFx Project" item in your workspace.

The next step is to delete the "JavaFX SDK" from the "Classpath" module (Properties -> Java Build Path -> Libraries) and add the Maven dependency in order to remove compile errors:

<dependency>
   <groupId>org.openjfx</groupId>
   <artifactId>javafx-controls</artifactId>
   <version>11.0.1</version>
</dependency>

The last step is to add a VM argument to "Run Configuration" of your JavaFX class for starting the application (Run Configurations -> Arguments tab -> VM arguments):

--module-path C:\path to the OpenJFX\javafx-sdk-11.0.1\lib --add-modules=javafx.controls

and it should work after you run it. Of course, you should download the JavaFX SDK before that (https://gluonhq.com/products/javafx/), unzip it and use the path to "lib" folder in the aforementioned VM argument.