Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • When importing java.io, java.util, or anything similar, the error:

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

  • Existing classes have an error:

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

  • Other random "cannot resolve to a type" errors.

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:

  • OS: macOS Sierra

  • VS Code version 1.15.1

  • JDK version 1.8.0.144

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?

like image 290
Reed Hermes Avatar asked Aug 17 '17 19:08

Reed Hermes


People also ask

How do I fix imports in VS Code?

Set the correct Python path in VSCode In order to fix Unresolved Import in VSCode, you have to set python. pythonPath key in the settings to the correct value. You can quickly open the settings. json editor by accessing File > Preferences or press Ctrl + , key combination.

Why is Java not working on Visual Studio Code?

Try to update Language Support for Java to the latest, and then try step 3 to rebuild the workspace. If it doesn't work, then try to install an older JDK version, set its installation folder to "java. home" user setting in . vscode/settings.

How do I import a Java project code into Visual Studio?

You can directly import existing Java projects and modules to your workspace through File > Open Folder... (Make sure the opened folder contains your build tool scripts, for example, pom. xml or build. gradle ).


2 Answers

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).

like image 102
toreyhickman Avatar answered Oct 10 '22 15:10

toreyhickman


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.

like image 36
Afzaal Ahmad Zeeshan Avatar answered Oct 10 '22 15:10

Afzaal Ahmad Zeeshan