Installing Java on macOS

  1. Install Homebrew if not already installed:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install the latest JDK:
    brew install openjdk
    
  3. 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
    
  4. Add Java to your PATH in ~/.zshrc or ~/.bash_profile:
    echo 'export PATH="$(brew --prefix)/opt/openjdk/bin:$PATH"' >> ~/.zshrc
    

Alternative: Direct Download

  1. Download the JDK installer from Oracle’s website or AdoptOpenJDK
  2. Run the installer package and follow the installation prompts
  3. Verify installation:
    java -version
    

Installing Visual Studio Code

  1. Download VSCode from code.visualstudio.com
  2. Drag the downloaded application to your Applications folder
  3. Open VSCode

Configuring VSCode for Java Development

  1. 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
  2. Install additional recommended extensions:
    • Checkstyle for Java
    • SonarLint
    • Gradle for Java
  3. 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)

Verifying Your Setup

  1. Create a new file HelloWorld.java:
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, Java!");
        }
    }
    
  2. Run the file by clicking the “Run” button above the main method or by right-clicking in the editor and selecting “Run Java”

  3. 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
  • If extensions are not working properly:
    • Update extensions to the latest version
    • Reload VSCode window (Cmd+Shift+P → “Developer: Reload Window”)