YulkyTulky
YulkyTulky

Reputation: 1086

Visual Studio Code Cannot Handle Java

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.

Upvotes: 14

Views: 15253

Answers (5)

Hyewon Seok
Hyewon Seok

Reputation: 21

I was in the same situation.

In my case, I changed owner of my project as follows since its owner was root.

chown -R user:group my-project

Then all errors disappeared in vscode.

Upvotes: 0

Alex Richards
Alex Richards

Reputation: 1

Important: this only applies to gradle users

I experienced the same error when I attempted to use a more recent version of JavaFX via gradle, without also specifying the newer version of the java toolchain. Make sure the versions match in your build.gradle.kts file, see the relevant sections below.

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

javafx {
    modules("javafx.controls", "javafx.fxml")
    version="21"
}

This error may also be caused by a broken system Java installation.

Upvotes: 0

Mark Hamlin
Mark Hamlin

Reputation: 348

I had this problem after updating the JDK from 17.05 to 17.07. Other projects worked with same jdk setting, other IDEs worked with same JDK and problematic project. Checked vscode settings in project directory, nothing wrong there. I was reluctant to follow the advice to delete $HOME/Library/Application Support/Code.

In the end, I tried renaming the project folder and reopened the project. By some magic, it worked.

Upvotes: 0

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.

Upvotes: 3

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9461

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

Upvotes: 10

Related Questions