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
Code | 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.
Code in JavaFXPad |
---|
|
Wrap the above program to use the command line as follows.
Code for Command Line |
---|
|
For the Clock_Example the wrapper, which could be included at the bottom of the file or separately, would look something like below.
Wrapper Code |
---|
|
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