Simple Javafx application for Raspberry PI 3

If you want develop desktop application for Raspberry PI, but don’t know how, then read below.

1. Download last version OS “raspbian stretch with desktop” from raspberry site.

Writing an image to the SD card, using instruction from that site.

2. Launch intellij idea and create simple Java application:

3. Create new class “Main” and write simple program:

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        double width = visualBounds.getWidth();
        double height = visualBounds.getHeight();

        Label label = new Label("Click the button.");
        label.setTranslateY(30);

        Button button = new Button("Click me");
        button.setOnAction(e -> label.setText("You clicked the button!"));

        Rectangle rectangle = new Rectangle(width - 20, height - 20);
        rectangle.setFill(Color.LIGHTBLUE);
        rectangle.setArcHeight(6);
        rectangle.setArcWidth(6);

        StackPane stackPane = new StackPane();
        stackPane.getChildren().addAll(rectangle, button, label);

        Scene scene = new Scene(stackPane, visualBounds.getWidth(), visualBounds.getHeight());

        stage.setScene(scene);
        stage.show();
    }
}

 

4. Create runnable jar.

Press Ctrl+Alt+Shift+S for open “Project Structure” window. Click on the “Artifacts” tab and after that click on the green plus and select Add JavaFx Application -> From module. For details look at the screenshot bellow.

After that go to the “Java FX” tab, select your “Main” class and press “OK”.

Now you can build application, from main menu press “Build” and then “Build Artifacts…”

At this moment you can find generated jar file in the projec location folder. Easy way to open folder with jar file:

Now you can test your program, type next command in the terminal window:

java -jar SimpleJavaFXApp.jar

5. Move jar file to Raspberry and test him there.

Move jar file to Raspberry and test it there with the same command.

It doesn’t work there, you can see error message. This because Oracle decided to drop the support for JavaFX on the ARM distribution of their JDK, and stopped shipping it within as well. For solve this problem you can downloaded the current version of JavaFXPorts (JavaFX Embedded SDK Version 8.60.8 armv6 hard float) from Gluon.

After that unzip downloaded file and move this folder on raspberry. Identify your java location with next simple command:

update-java-alternatives -l

And copy downloaded before libraries and other files in to your java location with next commands:

cp armv6hf-sdk/rt/lib/ext/jfxrt.jar /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/ext/
cp armv6hf-sdk/rt/lib/arm/* /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/arm/
cp armv6hf-sdk/rt/lib/javafx.platform.properties /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/
cp armv6hf-sdk/rt/lib/javafx.properties /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/
cp armv6hf-sdk/rt/lib/jfxswt.jar /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/

After that run your jar program again:

You can see warning messages like “Cannot open display” or “Cannot create resource”, but don’t worry your program work fine and if you use hdmi monitor with your raspberry, you can see your program window on screen.

1 Comment

  • Insalaco says:

    Hello,

    Thank you so much for this tuttoriel !

    I have just one issue.. the application work on the raspberry PI but i cant use my mouse for use the application..

    (I made my application with scene builder and intilij (JAVAFX))

    Do you have an explanation??

    Best regards,

    Insalaco Gioacchino

Leave a Comment