Planet JFX

Introduction

JavaFX applications can be developed using the command line like any other Java application although it is highly recommended to use an IDE (such as NetBeans) or JavaFXPad. The Objective of this tutorial is to show that it's possible to develop JavaFX applications using command line and some of the caveats with using these tools.

Steps to follow

Download the JavaFX Shell

Download the command line utilities at https://openjfx.dev.java.net/servlets/ProjectDocumentList and extract the zip file which will create a trunk/bin subdirectory containing the JavaFXShell that can be used to execute JavaFX programs.

Include JavaFX Shell in the PATH

Include the sub-directory that contains javafx.sh and javafx.bat which is in the trunk/bin sub-directory of the directory that was used to extract the zip file as part of the PATH

On Unix/Mac systems

For instance, if you are using bash you would do something like this:

export PATH=$PATH:/<path_to_where_you_unzipped_file>/javafx/trunk/bin

On Windows systems

You would do something which looks like

PATH=%PATH%;C:\<path_to_where_you_unzipped_file>\javafx\trunk\bin

There is an additional step that needs to be done on Windows to be able to include the current directory in the classpath.

Edit javafx.bat in the trunk\bin subdirectory. The initial part of the following line


%java% %opts% %debug% %opengl% -classpath %~dp0../javafxrt/src/javafx

needs to be changed to something like

%java% %opts% %debug% %opengl% -classpath .;%~dp0../javafxrt/src/javafx

to include the current directory(.).

There is no such file called javafx.bat in the bin subdirectory or anywhere else in the javafx-sdk1.3 instal bundle

Create the JavaFX Program

Use your favorite editor to create the HelloWorld.fx program

HelloWorld.fx
Code Preview
import javafx.ui.*;

Frame {
    title: "Hell[[[[Media:Link title]]]]o World JavaFX"
    width: 200
    height: 50
    content: Label {
        text: "Hello World"
    }
    visible: true
}
Preview

Compile and execute

On Unix/Mac systems

javafx.sh HelloWorld.fx

On Windows systems

javafx.bat HelloWorld.fx

This should bring up the HelloWorld window as shown above.

Wrapping JavaFXPad produced programs to execute using the command line

If you have a program developed in JavaFXPad or NetBeans that does not use a Frame, you may have to wrap the code appropriately in a Frame and optionally a Canvas to be able to execute in the command line.

Group.fx
Code in JavaFXPad
// Using JavaFXPad
import javafx.ui.*;
import javafx.ui.canvas.*;

Group {
    content:
        [ Rect {
            x: 10
            y: 10
            width: 400
            height: 125
            stroke: blue
            strokeWidth: 3
        }]
}

Wrap the above program to use the command line as follows.

Group.fx
Code for Command Line
// For the Command Line
import javafx.ui.*;
import javafx.ui.canvas.*;

Frame {
content:
Canvas {
content:
Group {
    content:
        [ Rect {
            x: 10
            y: 10
            width: 400
            height: 125
            stroke: blue
            strokeWidth: 3
        }]
} // Group
} // Canvas
visible: true
} // Frame


For the Clock_Example the wrapper, which could be included at the bottom of the file or separately, would look something like below.

Group.fx
Wrapper Code
// Wrap Clock program for command line usage
    Frame {
        content: Canvas {
            content: Clock {ticking: true}
        } // Canvas
        visible: true
    } // Frame

Conclusions

We showed above that by following a few simple steps, it's possible to develop a JavaFX application without using an IDE or JavaFXPad. However, development scenarios and requirements are more complicated and although this tutorial can be used as a basis, we recommend that you use an IDE for a more realistic development scenario as outlined in https://openjfx.dev.java.net/Getting_Started_With_JavaFX.html