How to Run Java files Using JavaFX

How to Run Java files Using JavaFX

  1. Download a JavaFX runtime for your operating system and unzip it to a desired location:

java_fx_unzip

  1. Add an environment variable pointing to the lib directory of the runtime. As I use oh my zsh on my Mac, I open the .zshrc file using vi .zshrc and add an envrionment:
1
2
# JavaFX
export PATH_TO_FX=/Users/leslietang/Documents/development/javafx-sdk-11.0.2/lib
  1. We can compile files by adding the modules:
1
2
3
javac --module-path $PATH_TO_FX 
--add-modules=javafx.controls
--add-modules=javafx.swing MyClass.java
  1. Likewise for the commands java and javadoc need to be extended. Therefore, you may want to use aliases. To add aliases, go to the folder ~/.oh-my-zsh/custom and create a new .zsh file(I use aliases.zsh), then add aliases to this file:

add_alias_for_javafx

  1. Now we can compile and run Java files using JavaFX:

javafx_run