Search code examples
javavisual-studio-codeapple-m1

Set proper path on Visual studio Code for Java on M1 MacBook Pro


so I recently got the m1 MacBook Pro and I was able to install home-brew and install java with it which installed Java-16. The problem I'm facing is how to configure java with Visual Studio Code on the m1 Mac. When I look for java installed location where java it gives me

/opt/homebrew/opt/openjdk/bin/java
/usr/bin/java

But when I put this as the path /opt/homebrew/opt/openjdk in Visual Studio Code, it doesn't recognize it and gives me no jdk installed on the machine. doing java --version gives me

openjdk 17 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode)

I'm confused how to configure this properly and how to find the actual path of java installed so I can configure it with Visual Studio Code.


Solution

  • Adding the following configuration to settings.json makes VS Code choose the correct Homebrew JDK for my Maven projects:

        "java.configuration.runtimes": [
        {
          "name": "JavaSE-17",
          "path": "/opt/homebrew/Cellar/openjdk@17/17.0.5/libexec/openjdk.jdk/Contents/Home",
        },
        {
          "name": "JavaSE-11",
          "path": "/opt/homebrew/Cellar/openjdk@11/11.0.17/libexec/openjdk.jdk/Contents/Home",
          "default": true
        }
      ]
    

    Just make sure that the configuration matches the actual versions you have installed.