Visual Studio Code Cannot Handle Java

Viewed 5427

I am trying to edit Java files in VSCode, but am encountering a slew of errors from VSCode. For example:

The type java.lang.Object cannot be resolved.
    It is indirectly referenced from required .class files
String cannot be resolved to a type
System cannot be resolved

I tried running Java clean to clean the workspace and have tried reinstalling the Java extension pack. Either way, the problem persisted.

I can't get rid of this and it is really annoying! Please help if you can! Thank you.

2 Answers

There's something wrong with JRE. In some occasions, deleting the Java Language Server workspace directory is helpful to go back to a clean status:

Windows - Delete %APPDATA%\Code and %USERPROFILE%\.vscode.
macOS - Delete $HOME/Library/Application Support/Code and ~/.vscode.
Linux - Delete $HOME/.config/Code and ~/.vscode.

If this doesn't work, try to reset jdk and restart vscode:

"java.home": "\path\to\jdk\",
"java.configuration.runtimes":[...]

Reference: Setting for the JDK

Molly Wang-MSFT's answer will definitely work but it will cause you to lost your other VS Code installed extensions and user settings. I will recommend you try uninstalling only "Extension for Java Pack" and reinstall it.

NOTE: If you have your codes opened via VS Code workspace, delete the workspace and recreate.

Related