Debug java in vscode
My computer is Mac M1.
show all the java version
/usr/libexec/java_home -V
we can see :
Matching Java Virtual Machines:
17.0.11 (x86_64) "Homebrew" - /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
1.8.0_391 (x86_64) "Java SE 8" - /Library/Java/JavaVirtualMachines/jdk1.8.0_391.jdk/Contents/Home
because the vscode need v17+ at least , so if you can't found 17+, you can intall it .
install jdk 17 or others.
brew install openjdk@8
brew install openjdk@11
brew install openjdk@17
they can coexist. so don't worry it will confused your envriments.
now we can show the env, if you can't see the jkd17 after exec brew install openjdk@17
, you need exec this:
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
and add the path to zshell
echo 'export PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH"' >> /Users/[your name]/.zshrc
OK , we edit the vscode configrations .
settings.json
{
// let Oracle Java extensions LSP to use 17
"jdk.jdkhome": "/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home",
// if you project always use the old version 1.8 , you can set this.
// "jdk.project.jdkhome": "/Library/Java/JavaVirtualMachines/jdk1.8.0_391.jdk/Contents/Home"
}
the next, we need to install plugins to better support java
- Java Extension Pack
- Language Support for Java(TM) by Red Hat
- Debugger for Java
- Maven for Java(if use Maven)
- Gradle for Java(if use Gradle)
- Lombok Annotations Support for VS Code
for Lombok , if your @Data or @Setter / @Getter don't working , we need to set the config path. first find the path
find ~/.m2/repository -name "lombok-*.jar"
if we install lombok by maven , so we can see:
/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.26/lombok-1.18.26.jar
/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.24/lombok-1.18.24.jar
/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar
/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30-sources.jar
/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30-javadoc.jar
edit .vscode/settings.json
add:
{
"java.compile.nullAnalysis.mode": "automatic",
"jdk.jdkhome": "/opt/homebrew/Cellar/openjdk@17/17.0.16/libexec/openjdk.jdk/Contents/Home",
"jdk.project.jdkhome": "/Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home",
"java.jdt.ls.vmargs": "-javaagent:/Users/[your name]/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar"
}
now, reload the vscode. all done.