Reed Hermes
Reed Hermes

Reputation: 2236

Visual Studio Code - Java - Import Errors and More

Just starting working on an existing project at work and wanted to use Visual Studio Code as my IDE (I have used it for a recent Rails project and loved it, so wanted to try with Java).

However, whenever I try to open one of the projects I receive a ton of different errors including:

"The import of java.io (or java.util) cannot be resolved"

"The implicit super constructor is undefined for default constructor. Must define explicit constructor"

All of these seem to stem from some sort of setting error I have with VS Code but can seem to find what it is. I have already uninstalled and reinstalled the RedHat plug-in that enables the Java language for VS Code.

I have tried setting the java_home setting in the extension to the direct location of the install but that didn't work. Tried uninstalling and reinstalling java and that also didn't work.

My operating specs are as follows:

Any idea on what may be causing this? Do you think it may be an error in how I have VS Code set up or if it's an error (or rather incompatibility) with how the existing project is set up?

Upvotes: 116

Views: 188712

Answers (13)

charlescol
charlescol

Reputation: 45

In my case, with Gradle and a custom configuration in the build.gradle, Clean Java Language Server Workspace did not work.

The problem was the following:

The build.gradle contained a custom configuration. By default, many IDEs (including VS Code with the Java extensions) do not automatically pick up libraries that are only declared in a custom configuration. They expect the dependencies to appear in one of the standard configurations.

How to Fix in that case:

  • Declare dependencies in both the custom configuration and a standard configuration
  • Switch everything to a standard configuration

Upvotes: 0

mipyon
mipyon

Reputation: 11

I had the same issue but the first answer didn't work. Adding the JAVA_HOME in the settings.json of VScode worked for me.

to open the settings.json: ctrl+cmd+p then type Preferences: Open User Settings (JSON). Add a new line with

"java.jdt.ls.java.home": "<your $JAVA_HOME>",

save and reload Vscode

Upvotes: 1

Rishabh Titvilasi
Rishabh Titvilasi

Reputation: 33

I was facing the same problem, I moved the Visual Studio Code app from Downloads folder to Applications folder and then restarted visual studio and ran java clean. Started working for me.

Upvotes: 0

brglnd
brglnd

Reputation: 11

The suggested solution did not work for me, however I solved it by going to my settings.json (CMD + Shift + P -> "Preferences: User Settings") and add the following

"java.configuration.runtimes": [

{
  "name": "JavaSE-18",
  "path": "/Users/[username]/Library/Java/JavaVirtualMachines/openjdk-18.0.1.1/Contents/Home",
  "default": true
},

where the path to your JDK could be something else of course

Upvotes: 1

forever
forever

Reputation: 51

I solved it by going to my settings.json and add the following

    "java.jdt.ls.java.home": "/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home"

Upvotes: 5

Rohit Chavan
Rohit Chavan

Reputation: 1

Nothing can do, just open this file into new folder and after that run it. It should prperly run now.

Upvotes: -2

Chetan Gupta
Chetan Gupta

Reputation: 152

Press ctrl+shift+p then search 'java clean' and click 'java: clean java language server workspace' then click restart IDE.

Upvotes: 6

TigerBear
TigerBear

Reputation: 2854

For me: CMD + Shift + P Then type "Java: Clean Java language Server Workspace"

Note: This will reload/restart vscode as well.

Update: This appears to not fix it anymore for me. In my case I am using a gradle project, and needed to set the rootProject.name in the settings.gradle to be the same as the folder name that the project is in.

Upvotes: 21

questionto42
questionto42

Reputation: 9630

I found another simple trick at least to get rid of "cannot be resolved to a type" errors which were coming from older workspaces and wrong project files I guess?. I just ran an empty main(), with the body commented out, while still keeping my local package / import commands at the start - no errors. After commenting in again, the project compiled without errors. Perhaps this refreshing effect might also help in this context?

Upvotes: 1

LucianoBAF
LucianoBAF

Reputation: 439

I faced this issue after creating a whole Java project in one computer and then trying to run it on another computer.

After doing everything said in the other answers, what really made VS Code compile was to open each single project java file in VS Code and save it (a simple Ctrl + S). Maybe there is a simpler way of doing it, but that is what worked for me and I hope this helps anyone stuck in this issue.

Upvotes: 4

Arjun Shetty
Arjun Shetty

Reputation: 1585

I had to clean this folder to get it working on Windows

%APPDATA%\code\Local Storage

Upvotes: -1

toreyhickman
toreyhickman

Reputation: 2594

I ran into a similar issue. The solution was to remove everything from VS Code's workspace storage directory, which was located at $HOME/Library/Application Support/Code/User/workspaceStorage/.

I found this solution here: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting#clean-the-workspace-directory

Update: This can now be done from within VS Code as of Language Support for Java(TM) by Red Hat Version 0.33.0. Open the command palette and type "java clean" (see official description in link).

Upvotes: 240

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

As already mentioned previously, you require to clean the project, but that is a bit difficult thing because every folder is a Guid, and you do not know which one to clear, thus requiring you to delete everything. Starting with 0.33.0 version of the plugin you can automatically do that from within the IDE as well, use CTRL + Shift + P and type, java clean, and IDE will show you the suggestion tip for, Java: Clean the Java language server workspace. Upon selection, agree and restart the IDE. It will clean the language server workspace for you.

Another approach can be, the Maven tools within the IDE. If you have this plugin installed, you can use the side bar and utilize the Maven project helper options to perform actions like, clean, install, and package etc. For example, here is the project I am having and the options this shows,

enter image description here

That can be used, graphically, to manage your Maven-based projects. Also, this would work with the Java Extension Pack, not sure yet as to how it would behave with other extensions.

Upvotes: 47

Related Questions