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 option 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
javac
that you haven’t seen before that we explain below:-p $JAVAFX_HOME/lib
: tells the java compiler where to find the JavaFX library files (.class
files) on Odin.--add-modules javafx.controls
: JavaFX is organized into 7 different modules. This option tellsjavac
which modules to include. A list of the modules in JavaFX 17 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 cs1302.gui.ExampleDriver
Notice the use of the
-p
andadd-modules
command line arguments as shown in the previous step.If the previous command was successful (it may not be and that’s okay), then one of the following things should happen:
A small GUI app containing the text “Hello World!!!” will appear on your screen. This may appear in another desktop or minimized if your computer utilizes multiple monitors.
A small GUI app containing the text “Hello World!!!” will appear on your screen. This may appear in another desktop or minimized if your computer utilizes multiple monitors. However, the following error message will appear in the terminal:
libGL error: No matching fbConfigs or visuals found libGL error: failed to load driver: swrast Error in glXCreateNewContext, remote GLX is likely disabled
A longer error message similar to the following will appear and no GUI app will appear on your screen (don’t panic):
libGL error: No matching fbConfigs or visuals found libGL error: failed to load driver: swrast 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) Value in failed request: 0x0 Serial number of failed request: 23 Current serial number in output stream: 24
If you receive an error message in the terminal, then this error message is related to the JavaFX graphics renderer. Rerun the driver and make sure you correctly typed the
-Dprism.order=sw
option properly. You may still receive a slightly shorter error message but your application will run smoother (less lag) with this option set.If the small GUI app containing a nice message appears with or without a small error message, then you are okay to proceed! Note: You may have to wait a few seconds to see the message.
Note
If you are running MacOS and get an exception message related to
DISPLAY
the first or second time that you attempt to run the program, then please ensure that you have followed the instructions provided at the beginning of the semester for MacOS users. In particular, you need to install XQuarts via Homebrew and restart after the installation completes.Congratulations on compiling and running your first graphical application on Odin! In the next two sections, we will walk through the code to help you understand how it works!