Ever since JDK 11, JavaFX has been decoupled from the default Java JDK installer, so it has to be installed and configured separately. Here's how.
Click the apple logo, and select "about this mac". Under "processor" if it says INTEL, then it is x64. If it says Apple, then it is aarch64 (ARM).
Go to GluonHQ and download the most recent version for JavaFX Mac OS X SDK (either x64 or aarch64 depending on your machine). Once it's downloaded, copy the contents of the .zip file (command-c and command-v) into your CS 120 folder (which is probably something like: /Users/yourUsername/Documents/CS120/
). Now you should have a folder named something like javafx-sdk-17.0.1
inside your CS 120 folder, except your version number will be higher.
Open your text editor as usual. Create a new file and save it as Compile.command
in whatever JavaFX project folder you are working on (e.g. lab13
). Paste in the following as the contents of the file.
#!/bin/bash javac --module-path /Users/yourUsername/Documents/CS120/javafx-sdk-17.0.1/lib/ --add-modules javafx.controls,javafx.graphics,javafx.media,javafx.fxml *.java
Now modify the /Users/yourUsername/Documents/CS120/javafx-sdk-17.0.1/lib/
part to match where you placed the folder and what version number you are using. Be sure to keep the /lib/
part at the end.
The Compile.command
script your just created will compile all .java files in that folder. To run it type in the command ./Compile.command
at the terminal window instead of the usual. Try it and see if it produces the .class file for your program.
Running your program is similar. Create a file named Run.command
in the same folder again, and inside it put the following code (with the file path and name adjusted as before), and change House
to whatever the name of your program is.
#!/bin/bash java --module-path /Users/yourUsername/Documents/CS120/javafx-sdk-17.0.1/lib/ --add-modules javafx.controls,javafx.graphics,javafx.media House
This script will run whatever java program is named at the end of that line. To run it, type in ./Run.command
in the terminal (after compiling). Try it and see if it runs. If you need an already-working JavaFX program to try this out on, you can use this Java program, which displays a black circle on a white background.
Here are some solutions to common problems:
Run.command
and Compile.command
files in the same folder where your .java file is.Run.command
, so for example if you are trying to run JavaFXSample.java
, then your Run.command
file's second line should end with JavaFXSample
instead of House
.chmod u+x Compile.command
chmod u+x Run.command
Whenever you start a new project, copy both of your command files into the new folder, and be sure to go into Run.command
and change the name of the program.