While it certainly makes it easier to build JavaFX with the support of an IDE (see the NetBeans and Eclipse recipes), it is not a requirement. In some situations, having direct access to the SDK tools is preferred (automated build for instance). This recipe explores the build tools that are shipped with the JavaFX SDK and provides steps to show you how to manually compile your applications.
To use the SDK tools, you will need to download and install the JavaFX SDK. See the recipe Installing the JavaFX SDK for instructions on how to do it.
Open your favorite text/code editor and type the following code. The full code is available from ch01/source-code/src/hello/HelloJavaFX.fx
.
package hello; import javafx.stage.Stage; import javafx.scene.Scene import javafx.scene.text.Text; import javafx.scene.text.Font; Stage { title: "Hello JavaFX" width: 250 height: 80 scene: Scene { content: [ Text { font : Font {size : 16} x: 10 y: 30 content: "Hello World!" } ] } }
Save the file at location hello/Main.fx
.
To compile the file, invoke the JavaFX compiler from the command line from a directory up from the where the file is stored (for this example, it would be executed from the src
directory):
javafxc hello/Main.fx
If your compilation command works properly, you will not get any messages back from the compiler. You will, however, see the file HelloJavaFX.class
created by the compiler in the hello
directory.
If, however, you get a "file not found
" error during compilation, ensure that you have properly specified the path to the HelloJavaFX.fx
file.
The javafxc compiler works in similar ways as your regular Java compiler. It parses and compiles the JavaFX script into Java byte code with the .class
extension.
javafxc accepts numerous command-line arguments to control how and what sources get compiled, as shown in the following command:
javafxc [options] [sourcefiles] [@argfiles]
where options
are your command-line options, followed by one or more source files, which can be followed by list of argument files. Below are some of the more commonly javafxc arguments:
classpath (-cp) - the classpath option specifies the locations (separated by a path separator character) where the compiler can find class files and/or library jar files that are required for building the application.
javafxc -cp .:lib/mylibrary.jar MyClass.fx
sourcepath - in more complicated project structure, you can use this option to specify one or more locations where the compiler should search for source file and satisfy source dependencies.
javafxc -cp . -sourcepath .:src:src1:src2 MyClass.fx
-d - with this option, you can set the target directory where compiled class files are to be stored. The compiler will create the package structure of the class under this directory and place the compiled JavaFX classes accordingly.
javafxc -cp . -d build MyClass.fx
When specifying the source files, you can use the wild card characters to indicate multiple source files to be compiled as follows:
javafxc -d build src/*.fx
The @argfiles option lets you specify a file which can contain
javafxc
command-line arguments. When the compiler is invoked and a@argfile
is found, it uses the content of the file as an argument for javafxc. This can help shorten tediously long arguments into short, succinct commands.Assume file cmdargs has the following content
-d build -cp .:lib/api1.jar:lib/api2.jar:lib/api3.jar -sourcepath core/src:components/src:tools/src
Then you can invoke javafxc as:
$> javafxc @cmdargs