Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 347
YulkyTulky Avatar asked Sep 28 '20 14:09

YulkyTulky


People also ask

Why Java is not working in VS Code?

vscode/settings. json and reopen your VS Code workspace. Click F1 -> Java: Force Java compilation -> Full to rebuild the workspace. If it still doesn't work, then try F1 -> Java: Clean the Java language server workspace to clean the cache.

Can I run Java on VS Code?

Visual Studio Code allows you to debug Java applications through the Debugger for Java extension. It's a lightweight Java debugger based on Java Debug Server, which extends the Language Support for Java™ by Red Hat. Here's a list of supported debugging features: Launch/Attach.

How do I clear the Java Language Server workspace in VS Code?

Note: If you are modifying a project that was already opened in VS Code, you may need to force clean the workspace and reload. To do so, run command Java: Clean Java Language Server Workspace.

Could not find or load main class in Java VS Code?

The "Could not find or load main class" error occurs when the JVM fails to load the main class. This can happen due to various reasons, such as: The class being declared in the incorrect package. The file path of the class not matching the fully qualified name.


1 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

like image 124
Molly Wang-MSFT Avatar answered Oct 04 '22 21:10

Molly Wang-MSFT