38

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.

9
  • 1
    Did you follow the getting started guide? What steps were you able to perform successfully? and then where did it fails? Commented Sep 3, 2018 at 8:10
  • 1
    A similar question for intelliJ with a similar suggestion. Commented Sep 3, 2018 at 8:20
  • unable to run from eclipse Commented Sep 3, 2018 at 8:35
  • 1
    What is your Eclipse version? I have it working with 4.9M3 Commented Sep 3, 2018 at 8:53
  • Eclipse Photon(4.8.0). Did you able to run fx application ? Commented Sep 3, 2018 at 9:45

6 Answers 6

62

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.

  1. 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

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

Sign up to request clarification or add additional context in comments.

10 Comments

Done. i Have one more query how we can avoid giving module path in VM arguments, is there any way i can read the modules from repository ?
For that you can use Maven or Gradle, where the JavaFX dependencies are managed automatically via a local repo. See the mentioned guide for a Maven setup.
Could you explain " Install JDK 11 from here" ? The link points to a .zip file. Do you mean just unzip it and place it somewhere it can be used for step 3 ?
You could install Oracle JDK 11, or you can install Open JDK 11 from that link, without an installer, so just unzip it to your preferred location.
@AndrewS See openjfx.io/openjfx-docs/#IDE-Eclipse, once you download the JavaFX SDK there is a lib folder with the jars for each module: javafx.base.jar, ...
|
16

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 Comments

Where should Application.launch(MyApplication.class); go? I think you need to give an example of the entire file.
2b) is a really nice observation which saved me a lot of time - thanks!
2b) works for me also, but it produces this warning: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @eef29c6'
9

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

1 Comment

Maybe the good people of this post could take a look at this: stackoverflow.com/questions/55689045/… Best regards!
5

As mentioned by @Cristian, creating a separate Main class, seems to do the trick, without any other arguments.

public class Main {
    public static void main(String[] args) {
        Application.launch(MainWindow.class);
    }
}

public class MainWindow extends Application {
    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    // do not use this method
    //  public static void main(String[] args) {
    //      Application.launch(MainWindow.class);
    //  }
}

Comments

1

@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>

JavaFX SDK for deletion

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.

1 Comment

Why should one have to manually download an entire Java Fx SDK when dependency management is supposed to be the job of Maven?
1

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. Note, it is important that the JavaFX SDK is installed in a location without spaces in the path so C:/Program Files is not a good choice.

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.