Visual Studio Code Cannot Handle Java
Asked Answered
I

5

14

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.

Immensurable answered 28/9, 2020 at 14:10 Comment(4)
What do you mean by edit java files? edit .java or .class. if you edit .class you transfered it to bytecode or just edit binary file? explain more what you want to do.Statesmanship
@majidhajibaba I am editing Java source code files as I am taking a Java course in schoolImmensurable
did you use javac .java to compile? Once you change the file, it's necessary to compile it again before run.Veteran
@MollyWang yes, the problem does not have to do with compilation ... the programs run fine, my issue is that the VSCode IDE does not seem to read any of the Java correctlyImmensurable
V
10

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

Veteran answered 30/9, 2020 at 3:27 Comment(3)
setting "java.home": "/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home" worked for me!Immensurable
I had to use the Java: Configure Java Runtime wizard to get it to work. I downloaded adoptopenjdk-15.jdk and point VSCode to it.Fogg
-1, do not delete the folder as it contains not just the settings for Java Language Server, but everything for your VS Code instance. Maybe try to reset the JDK and restart the VSCode first and then go for a nuke approach.Elector
S
2

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.

Sandhurst answered 22/7, 2022 at 9:43 Comment(0)
P
0

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.

Prospect answered 30/6, 2023 at 11:59 Comment(0)
K
0

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.

Kingkingbird answered 9/11, 2023 at 13:31 Comment(0)
C
0

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.

Cuman answered 20/5 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.