Avatar ·

How to install and switch Java (JDK) version on macOS?

Hi everyone! I'm a beginner Java developer working on macOS. I have several JDK versions installed: 11, 17, and 21. The problem is that when I check the version via terminal, the system always shows the same one — 11, even though I need 17 for my project. I tried changing the JAVA_HOME variable manually via export, but it resets after restarting the terminal. I also downloaded several JDKs via Homebrew, then removed them, and now the list in /usr/libexec/java_home -V shows a mess. Please advise how to properly set up the environment so I can easily switch between versions? I want the change to be global (for the entire terminal) and persist after restart. Here's what the command currently outputs: ```bash $ java -version openjdk version "11.0.18" 2023-01-17 LTS ``` But I need