Setting up java11 javafx development environment on Windows 10

In light of the recent change the licensing for Java as provided by Oracle, it has necessarily become the case that developers (unless they are paying Oracle customers) should switch to OpenJDK. Oracle has made it clear that unless you are using their version of Java for personal use, or are a paying customer, you should not use their java.

If you are a Windows user, you might not have heard of OpenJDK. Don’t worry, I will walk you through the process of switching to OpenJDK 11.

From my experience, the best place to get OpenJDK as a Windows user is adoptopenjdk.net. If you are 64 bit, go here.

After downloading and installing the appropriate jdk, you will then want to navigate to this website. Download the Windows SDK, then extract the folder to someplace memorable like your Documents directory.

If openjdk was installed correctly, it should show up when using the command

java -version

in command prompt.

Eclipse should also show OpenJDK 11 when you make a new Java Project.

If you are not familiar with Java after Java 8, you may be surprised to learn that Java has a new module system. When prompted, go ahead and accept the automatic creation of module-info.java.

Your new project should look somewhat like this.

Observe that the module file should not have been named “module” because module is actually a new keyboard. If you named your module “module”, please change it.

It is good practice to use packages. I created one called my package and inside it I created a new class which I named simply “Program.”

Although not strictly necessarily for a simple Hello World program, you should add an “exports” line to your module file.

module myModule {
	exports myPackage;//name of the package
}

Now return to your class file and let’s make it reflect a basic javafx application in order to test it.

package myPackage;

public class Program extends Application {

	public static void main(String[] args) {
		launch(args);
	}
}

As you can observe, Eclipse isn’t going to have any suggestions for what do do about launch or Application because it doesn’t understand what Javafx is. After Java 8, Javafx was decoupled from Java so that it could be developed separately from java. That was why we had to download it separately.

Right click on the name of the project and press properties.

Then go to Java Build Path

Select Modulepath and then press “Add External JARs…”

Select all the jars

Observe that the suggestions do work correctly now. Accept the import suggestion for javafx.application.

Let’s write some more code to test the application. Be sure to allow Eclipse to update the module-info file so that the right requires statements are in there.

package myPackage;

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

public class Program extends Application {

	public static void main(String[] args) {
		launch(args);
	}

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

If the program runs, it should look like this:

Feel free to continue to tinker around. If it didn’t run, make sure that you have the exports line in your module-info file.

One comment

Leave a comment