Java
Installing OpenJDK on macOS
To set up OpenJDK on a macOS system, follow these steps:
Install OpenJDK with Homebrew:
brew install openjdk
Create a symbolic link:
Establish a link to make Java accessible system-wide.
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
Update the PATH environment variable:
Ensure the OpenJDK binaries are prioritized when executing Java commands.
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc
Set C++ flags:
This is necessary if you're compiling C++ programs that interact with Java.
export CPPFLAGS="-I/opt/homebrew/opt/openjdk/include"
After these steps, restart your terminal or run source ~/.zshrc
to apply the changes.
To compile and run a simple Java program, follow these steps
Create a Java file named
Main.java
with the following content:public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Open a terminal and navigate to the directory containing
Main.java
.Compile the Java program:
javac Main.java
Run the compiled Java program:
java Main
Ensure the file is saved with the correct syntax to prevent errors.
Compile and Run a JavaFX Application on Mac
To compile and run a JavaFX application on Mac, ensure the filename matches the public class
name.
Compile:
javac --module-path "./javafx-sdk-23.0.2/lib/" --add-modules javafx.controls,javafx.fxml [YourJavaFile].java
Run:
java --module-path "./javafx-sdk-23.0.2/lib/" --add-modules javafx.controls,javafx.fxml [YourJavaFile]
Replace [YourJavaFile]
with the name of your Java
Last updated