Setting up java11 javafx development environment on ubuntu 18.04

For this guide, I will be using openjfk and openjfx. A lot of guides and stackoverflow threads will suggest that you should simply install the following:

sudo apt install openjdk-11-jdk openjfx

And then to install Eclipse. HOWEVER: this did not work for me. It may have been because of the fact that the openjfx package on Ubuntu 18.04 is currently broken.

The openjdk-11-jdk package does work, however, and if it does not show up in Eclipse you may have to update-alternatives.

sudo update-alternatives --config java

How on Earth do I get javafx to work with java11 on Ubuntu 18.04?

Do it the right way. Get it from gluon. Download the appropriate package (See the following picture to see what I selected).

Then extract the zip archive and put the extracted directory where-ever you’d like, preferably in your Documents directory.

In Eclipse, go to Window > Installed JREs
If you do not see java11 there, you will have to add it. Assuming you installed java11 in the way I suggested, it should be located in /usr/lib/jvm/.

To add the appropriate installed JRE to the list of Installed JREs in Eclipse, press Add > Standard VM, press NEXT

Press Directory…

Then press navigate to /usr/lib/jvm and select the directory of the appropriate JRE.

In my case, I had to press where it said Filesystem in my file dialog as I couldn’t find a more Windows-esque manner to select the location.

Be sure that you have the java11 option selected and checked. Press “Apply and Close.”

First, to test Openjdk11 to make sure that it will even do a hello world.

Create a new java project in eclipse, don’t even bother calling it anything important. For good measure, do give it a module.

Create your test class in the src folder.

Write your program and test to make sure that it can properly output hello world via a println statement. If that works, then openjdk11 should be fine. Now comes the part where we make Javafx work.

As you can see, it does not know what javafx is and does not provide us with any options to automagically fix it. That’s OK. Right click on the project name (it is called “whatever” in my case”) and go to properties.

Select Modulepath, then select “Add External JARs…”

Navigate to where-ever you extracted the javafx-sdk that you got from gluon. In my case it was located in my documents. In the file-dialog, go into the lib folder and select all the jars.

Press Apply and Close


It will now tell us we have to add a requires line to the module file. We can now select “Add requires javafx.graphics…”

Now eclipse wants us to add the unimplemented methods. Let it add the start method and from there you can put some code in related to javafx so that you can test functionality. Here is some here.

The module-info.java file looks like this:

module whatever {
	requires javafx.graphics;
	requires javafx.controls;
}

The Test.java file that we have looks like this:

package whatever;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/** 
 * @author Seth 
 * 
 * In my case, it automatically made a package for me.
 */
public class Test extends Application {
	public static void main(String[] args) {
		System.out.println("Hello World");
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		
		var lbl = new Label("Hello World!");
		
		var box = new HBox();
		
		box.getChildren().add(lbl);
		
		Scene scene = new Scene(box);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}

If we try running it, you may observe that it doesn’t work and that we just get exceptions up the wazoo. Look again at the module-info file, observe that it only has requires. You must add an exports line.

Now it should work. Happy Javafxin’!

One comment

Leave a reply to If you are a Java programmer, you should switch from Oracle to OpenJDK now!!! | Seth's blog Cancel reply