12.2. Compiling JavaFX Applications¶
Now that you've downloaded the starter code for this chapter, we will
focus on compiling and executing that code. We will need a few new
command-line options to javac and java to make sure they can
find the necessary .class files for the JavaFX libraries. Follow
along with the steps below to compile your GUI application:
Compile the source code using the following command:
javac -d bin -p "$JAVAFX_HOME/lib" --add-modules javafx.controls src/cs1302/gui/*.java
There are a few new command-line arguments to
javacthat you haven't seen before that we explain below:-p "$JAVAFX_HOME/lib": tells the Java compiler where to find the JavaFX library files (.classfiles) on Odin.--add-modules javafx.controls: JavaFX is organized into 7 different modules. This option tellsjavacwhich modules to include. A list of the modules in JavaFX can be found here.
Run the compiled code using the command below:
java -cp bin -Dprism.order=sw -p "$JAVAFX_HOME/lib" \ --add-modules javafx.controls \ --enable-native-access=javafx.graphics \ cs1302.gui.ExampleDriver
Notice the use of the
-pand--add-modulesoptions from the previous step. The--enable-native-access=javafx.graphicsoption permits the native access that JavaFX uses for graphics and windowing. This is an option forjava, notjavac, and must appear before the application class name. The-Dprism.order=swoption selects software rendering for this remote graphical application.Wait a few seconds for a small GUI application containing the text "Hello World!!!" to appear. Check behind other windows, on other desktops or monitors, and for a minimized window.
The terminal normally does not display another shell prompt while the application is running. The application runs in the foreground, so the shell waits for it to exit. Close the application window when you are finished; for this starter application, the prompt should return.
If no window appears or the application is unresponsive, focus the terminal and press Ctrl+C to interrupt the running program. Wait for the shell prompt before entering another command. You normally do not need to restart your terminal application or your computer.
If the application does not run as expected, use the troubleshooting guidance below. Once the window appears and you can close it to return to the shell, continue to the next two sections to learn how the code works.
12.2.1. Troubleshooting¶
12.2.1.1. Native-access warning¶
An older copy of the run command may produce a warning containing these lines:
WARNING: A restricted method in java.lang.System has been called
WARNING: Use --enable-native-access=javafx.graphics to avoid a warning for callers in this module
This warning alone does not mean that the application failed to start. Close the application, or interrupt it with Ctrl+C, before retrying with the updated run command above. Enabling native access addresses this warning; it does not fix a separate display or startup problem. See the JavaFX 25 run instructions for the native-access option.
12.2.1.2. No window and no shell prompt¶
Check for a hidden or minimized window first. If no window appears after a few seconds, press Ctrl+C in the terminal to interrupt the program. Preserve any terminal output, then check the display setup described below before retrying.
12.2.1.3. Display or X-forwarding errors¶
If an exception mentions DISPLAY or says that a display cannot be
opened, revisit Getting Started and its platform-specific setup
instructions. Ensure that your local X server is running and that you
connected to Odin with the documented X-forwarding options. If you need
to reconnect, first stop the running application and return to the shell.
Do not assign an arbitrary value to DISPLAY; the SSH connection
configures the forwarded display.
On macOS, follow the linked setup instructions for XQuartz, including any restart required after installation. That installation step is separate from recovering the terminal after running a JavaFX application.
12.2.1.4. Graphics renderer errors¶
Messages such as the following concern graphics rendering:
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
Error in glXCreateNewContext, remote GLX is likely disabled
You may instead see a longer message that includes:
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 149 (GLX)
Minor opcode of failed request: 24 (X_GLXCreateNewContext)
After closing or interrupting the application, check that your run command
includes -Dprism.order=sw exactly as shown above. If the application
window appears and responds despite these renderer messages, you can
continue. If no window appears or it remains unresponsive, seek help with
the details below.
12.2.1.5. Other errors or problems that persist¶
Different errors require different fixes. Preserve the full output rather than repeatedly changing unrelated command-line options. When asking for help, include:
The exact command you ran and its complete terminal output.
The output of
java --version, run on Odin after regaining the prompt.Your local operating system and terminal application.
Whether an application window appeared and whether it responded.
Whether closing the window or pressing Ctrl+C restored the prompt.