Setting Up Java Development Environment on macOS
Installing Java on macOS
Using Homebrew (Recommended)
- Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install the latest JDK:
brew install openjdk
- Set up the system to use the Homebrew-installed Java:
sudo ln -sfn $(brew --prefix)/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
- Add Java to your PATH in
~/.zshrc
or~/.bash_profile
:echo 'export PATH="$(brew --prefix)/opt/openjdk/bin:$PATH"' >> ~/.zshrc
Alternative: Direct Download
- Download the JDK installer from Oracle’s website or AdoptOpenJDK
- Run the installer package and follow the installation prompts
- Verify installation:
java -version
Installing Visual Studio Code
- Download VSCode from code.visualstudio.com
- Drag the downloaded application to your Applications folder
- Open VSCode
Configuring VSCode for Java Development
- Install Java Extension Pack by pressing
Cmd+Shift+X
and searching for “Java Extension Pack”- This includes:
- Language Support for Java
- Java Debugger
- Maven for Java
- Java Test Runner
- Project Manager for Java
- This includes:
- Install additional recommended extensions:
- Checkstyle for Java
- SonarLint
- Gradle for Java
- Configure Java Home in VSCode settings:
- Open Command Palette (
Cmd+Shift+P
) - Type “Preferences: Open Settings (JSON)”
- Add:
"java.home": "/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home"
(Update the path if using a different Java installation)
- Open Command Palette (
Verifying Your Setup
- Create a new file
HelloWorld.java
:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Java!"); } }
-
Run the file by clicking the “Run” button above the
main
method or by right-clicking in the editor and selecting “Run Java” - The output should appear in the integrated terminal showing “Hello, Java!”
Troubleshooting
- If VSCode can’t find Java:
- Ensure
JAVA_HOME
environment variable is set correctly - Try restarting VSCode after installation
- Check VSCode’s “Output” panel for Java extension logs
- Ensure
- If extensions are not working properly:
- Update extensions to the latest version
- Reload VSCode window (
Cmd+Shift+P
→ “Developer: Reload Window”)