cheatsheets devlog projects search

installing-java.md

Notes on Installing Java and JavaFX

2022-12-28 1

Some notes on setting up Java for my course.

Installing Java

First step is to get the JDK, the Java Development Kit.

https://jdk.java.net/19/

Get the zip file and extract it to C:\bin.

Now we need to add java to the environment so that windows knows about it.

This is a system variable.

JAVA_HOME =  C:\bin\jdk-19.0.1

You will also need to add the bin directory to the PATH so java can be used from the commandline.

 C:\bin\jdk-19.0.1\bin

Open the command prompt and simply type in java. This should result in the java help being printed.

Now we have Java ready to go.

Installing JavaFX

Next up, javafx, the GUI library.

https://gluonhq.com/products/javafx/

Download the SDK, the software development kit as a zip. Extract the zip to C:\bin as well.

Eclipse - The Editor

Download Eclipse as a zip package and we will do the same thing as before, extract it to C:\bin.

https://www.eclipse.org/downloads/packages/

We should now have the following structure in C:\bin:

C:\bin\jdk-19.0.1
C:\bin\javafx-sdk-19
C:\bin\eclipse

Hello World in Java

The first step is to make sure java is installed properly before we get to JavaFX.

To do this, make a hello-world project and quickly get something on the screen.

package helloworld;
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); 
    }
}

Hello World with JavaFX

The first step to getting JavaFX working with eclipse is to follow the below instructions:

https://openjfx.io/openjfx-docs/#install-javafx

A hello world example

https://docs.oracle.com/javafx/2/get_started/hello_world.htm

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class HelloWorld extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}