"Cannot resolve symbol" in Intellij IDEA - java

I'm fairly new to Intellij and Java and am unable to use libraries from one "module" in another. I am seeing red class names and getting "Cannot resolve symbol ..." errors all over the place:
The other modules are referenced in File | Project Structure all the other module's Paths are correct and include the *.classes I'm trying to use:
I have also tried a File | Invalidate Caches but am still having the same problems.
What do I need to do to fix this?

Listed below are steps which may fix the problem:
1.Invalidate and refresh the IntelliJ's caches
File Menu -> Invalidate Caches / Restart -> Invalidate and restart
2.Project SDK selection
Check project settings from File Menu -> Project Structure
Ensure an SDK is selected for the Project SDK. If is selected, click the 'New' button, select JDK and then select the correct path to the JDK e.g. /opt/java/jdk1.8 or c:\Java\Jdk1.8
3.Corrupt JDK
There may be a problem with your JDK installation, re-install JDK
4.Re-import the project
Close the project if it is open in IntelliJ
Delete the .idea folder in the project source code folder.
Start IntelliJ and Import the project.
You can choose any of them which will work for you.

I faced a similar problem for no reason, I think it was caused by IDE(android studio). I have tried all of the above solutions but not worked. Finally, in my own situation, I solved this problem by the following actions:
Delete your .idea folder in your local project.
Reopen this project and just let Gradle run itself.
It was worked for me. I hope that my experience will be useful for you.

You need to add your external libraries/classes under dependencies in order for IDEA to see them when doing code analysis/autocomplete.

You can go to "File->Project Structure"
From "Project Settings", Select "Project" tab and there make sure that "Project SDK" has proper JDK selection.

After you add dependencies to POM file, make it a habit to refresh POM by clicking on the 'Reimports maven projects' icon. Its the first icon in the maven tool window located to the right of the main intellij editor. Its a blue icon with 2 circular arrows. Clicking it ensures that the added dependencies are fully integrated into your project

Run it from the project's root folder.
$ mvn -U idea:idea

One more advise would be to make sure that you import the project properly. If it's a maven project, import pom.xml and if it's a gradle project, import gradle.build file to make sure that all the dependencies are downloaded correctly.

Just got this.
Just went there.
Invalidated the cache.
Seemed to work until I wrote further lines.
Then noticed that I was writing my code outside of the main() function.
Moved the ending curly bracket further and voila.

Related

Cannot resolve symbol - IntelliJ [duplicate]

This problem happens intermittently for different libraries and different projects.
When trying to import a library, the package will be recognized, but the class name can't be resolved.
If on the import statement, I right-click -> Goto -> the package's declaration, I see all the decompiled classes displayed in the side pane -- Including the ones I need --
If I try to auto-complete the import statement, I notice the class I need is not featured in the dropdown.
I tried invalidating caches already, doesn't work. I cannot find any class conflicts -- there is no other jar file in my classpath with the same package name.
I am able to import this class into other projects.
Please see screen shots:
Anyone have a clue?
You can try invalidating the cache and restarting IntelliJ, in many cases it will help.
File -> Invalidate Caches/Restart
There can be multiple reasons for this. In my case it was wrong source root issue. Invalidate caches didn't work along with other solutions.
Check your module source roots.
Project Structure (Ctrl+Alt+Shift+S).
Modules
Select your problem module.
Change tab on top of window "Sources".
Remove unwanted source roots. Keep one and add src and test source roots in this root.
File -> Invalidate Caches/Restart
And
Build your project
IntelliJ has issues in resolving the dependencies.
Try the following:
Right click on pom.xml -> Maven -> Reimport
Again Right click on pom.xml -> Maven -> Generate sources and update folders
Run this command in your project console:
mvn idea:idea
Done.
Had this issue many times. Tried 'Invalidate Cache & Restart' and all other solutions. Running that command works perfect to me.
I'm currently using IntelliJ 2019.2, but this also happened in previous versions and solution worked as well.
File -> Invalidate Caches/Restart or rebuilding the project did not work wor me.
What worked for my Gradle project was to "Refresh all Gradle projects" from the Gradle tab on top-right corner of IntelliJ v2017, using the yellow marked button shown below:
Check your module dependencies.
Project Structure (Ctrl+Alt+Shift+S).
Modules
Select your problem module.
Change tab on top of window "Dependencies".
Check what needed library (maybe, you need to add specified library
in the tab 'libraries') or module has listed here and it has right
scope ('complile' mostly).
I faced a similar issue, Mike's comment helped me move in the direction to solve it.
Though the required library was a part of the module in my project too, it needed a change of scope. In the module dependency, I changed the scope to "Compile" rather than "Test" and it works fine for me now.
Had the same problem till I noticed that the src folder was marked as root source instead of java!
Changing to only the java (src/main/java) to be the source root solved my problem
I found the following answer from #jossef-harush and #matt-leidholm useful from another link
in IntelliJ editor, click on the red keyword (Integer for example) and press ALT + ENTER (or click the light bulb icon)
select Setup JDK from the intentions menu
click on Configure
In my case, the JDK path was incorrect (pointed on /opt/jdk1.7.0_51 instead of /opt/jdk1.7.0_65)
click on the ... and browse to the right JDK path
let's clear the cache
Right click on pom.xml file, go to Maven click on Reimport. I had similar problem and this worked for me.
After a long search, I discovered that a dependency was somehow corrupted on my machine in a maven project. The strange thing was that the dependency was still working correctly in the compiled java code. When I cleaned and rebuilt my maven dependency cache however, the problem went away and IntelliJ recognized the package. You can do this by running:
mvn dependency:purge-local-repository
Intrestingly, the source of my problem hence wasn't IntelliJ, but maven itself.
For 2020.1.4 Ultimate edition, I had to do the following
View -> Maven -> Generate Sources and Update Folders For all Projects
The issue for me was the libraries were not getting populated with
mvn -U clean install from the terminal.
Try cleaning maven from upstream by:
mvn -U clean install
I also got this error for multiple times when I try to build a new java project.
Below is the step how I got this stupid issue.
Create an empty project, and create new directory src/main/java.
Create the source package net.gongmingqm10.sketch.
Use gradle wrapper, gradle idea to build the gradle stuff for the project.
Add some dependencies in build.gradle file, and gradle build, reimport the project.
Create User.java and School.java in the existing package net.gongmingqm10.sketch
I got the error while I try to use import School in User.java.
Intellij keeps complain can not resolve symbol on import.
Solution:
Build the project first, and mark the main/java as the source root. Create a new directory with the same name net.gongmingqm10.sketch. Move all the files from the old troubling packages to new package.
Root cause:
Directory tree of source code
As you can see from the tree. I got a directory named net.gongmingqm10.sketch. But what we really need is the 3 levels directory: net->gongmingqm10->sketch
But before I finish building my project in Intellij, I create new directory named net.gongmingqm19.sketch, which will give me a real directory with the name net.gongmingqm10.sketch.
When we trying to import it. Obviously, how can intellij import the file under the weired directory with the name a.b.c.
#Anton Dozortsev I was driven crazy by a similar behavior; I ended up re-installing the IDE a bunch of times, only getting past the issue after removing the IDEA app, all caches, preferences, etc.
I tried all kinds of steps in the interim, (like restarts, invalidations, deleting .idea and *.iml files, and others.)
Turns out, the problem was due to IntelliJ's idea.max.intellisense.filesize setting. I had set it to 100KB, which was smaller than my dependency's size, leading to IntelliJ showing it as unknown, just like in your screenshot.
Fix:
Click on Help -> Edit Custom Properties
Set the property to a larger value; the default is 2500KB
idea.max.intellisense.filesize=2500
Please try File-> Synchronize. Then close and reopen IntelliJ before you invalidate.
Once I restarted. I would have invalidated but the synchronize cleared everything after restarting.
Simple Restart worked for me.
I would suggest first try with restart and then you may opt for invalidating the cache.
PS : Cleaning out the system caches will result in clearing the local history.
I found the source cause!
In my case, I add a jar file include some java source file, but I think the java source is bad, in Intellij Idea dependency library it add the source automatic, so in Editor the import is BAD, JUST remove the source code in "Project Structure" -> "Library", it works for me.
What did it for me is to edit the package file in the .idea folder as I accidentally added sources to this jar library and android couldn't resolve it by deleting the sources line as marked in the b/m picture library error.
Then rebuild the gradle and bam problem solved.
I had the same issue and the reason for that was incorrect marking of the project's sources.
I manually created the Root Content and didn't notice that src/main/test folder was marked as Sources instead of Tests. So that is why my test classes were assumed to have all their test libraries (JUnit, Mockito, etc.) with the scope of Compile, not Test.
As soon as I marked src/main/test as Tests and rebuilt the module all errors were gone.
I had this recently while trying to use Intellij to work on NiFi, turned out the issue was that NiFi requires Maven >= 3.1.0 and the version that I'd checked out with (I guess my default) was 3.0.5. Updating the Maven version for the project fixed it, so in some cases Maven version mis-alignment can be a thing to look...I'd guess it's fairly unusual but if you get this far on the thread you're probably having an unusual issue :)
file-> Project Structure -> Modules, find the module with problems, click it and choose the Dependencies tab in the right side. Click the green plus sign, try to add the jar or libraries that cause the problem. That works for me.
Nothing I tried above worked for me (not that I tried every suggestion). What finally did the trick was to rename the class -- I just added a 2 to the class name and filename. Then I resolved all the references manually. (Since they weren't recognized, the refactoring did not change the references automatically.)
Once the "2-version" was happily resolved everywhere, I was then able to refactor and remove the 2 from the class and file, and everything was then as it should be.
in my case the solution was to add the project as maven project, besides the fact that i imported as maven project :P
go to pom.xml -> right click -> add as maven project
My issue was my Maven plugin got disabled after an update. I went to Help -> Find Action... -> Typed in Maven and found that it was "Off". I clicked the toggle switch and after a bit of loading it was re-enabled.
Also, check your class is not in compile exclusions
If you see, that there is a little grey cross in left top corner, you must remove class from compile exclusions
How to remove
Old question, '21 response. I ran into the issue where my go build would build code successfully but my Goland IDE showed missing modules or dependencies. I tried Invalidating Caches and Restart, but had the same problem. From another S/O thread, I tried adding the GO111MODULE=on to my Path Variables, but that didn't resolve the IDE problems either.
What worked for me was picking the correct GOROOT path in Preferences > Go > GOROOT.
I had two versions of go installed, one by brew and one from the online Go installer. I selected the brew install path, and my IDE was able to resolve the dependencies properly.
I've tried all the complicated methods and they didn't work, since I was too lazy to re-import the project I tried something else.
Mine is a gradle project, so I went to my gradle.build file, removed the dependency, refreshed the dependencies, then added the dependency again and refreshed again, the imports started working after that.
Faced similar issue,
I Updated Intellij and error start coming - Can't Resolve Symbols.
Went to Plugins, Updated the plugins & Restart
Problem Solved !!

The project cannot be built until build path errors are resolved [duplicate]

While compiling an android project in eclipse 3.4.2, I am getting The project cannot be built until the build path errors are resolved.
I got a temporary solution from the blog http://www.scottdstrader.com/blog/ether_archives/000921.html
The resolution was to force a resave of the selected projects (and their .classpath files):
Open the project properties
Select Java Build Path > Libraries
Add a new, arbitrary library (to be deleted later) > OK
Wait for the workspace to refresh (or force a refresh of the project)
The error(s) will go away
Remove the dummy library
The only other references I could find were to make minor alterations of contents of the .classpath file.
Is there any permanent fix for this issue?
Have you tried using Project > Clean... from the menu? This will force a new build on the selected projects in Eclipse.
1-Right CLick on your project folder, Choose Build Path > Configure Build Path
2-Select Libraries Tab and delete any arbitrary library present there.
3-Click on Add Library option, Select JRE System Library and click Next.
4-Choose last Radiobutton option Workspace default JRE and click Finish.
5-press f5 for refresh.
6-run ur program .
This what fixed it for me...
I was having an issue with my spring-core.jar.
I deleted the entire release directory located here. (I'm on win 10).
C:\Users********.m2\repository\org\springframework\spring-core\4.3.1.RELEASE
I right clicked on the project > Maven > Update project and my exclamation mark disappeared. No problems any more.
Here is the source where I found the information:
http://crunchify.com/cannot-be-read-or-is-not-a-valid-zip-file-how-to-fix-maven-build-path-error-with-corrupted-jar-file/
This happens when libraries added to the project doesn't have the correct path.
Right click on your project (from package explorer)
Got build path -> configure build path
Select the libraries tab
Fix the path error (give the correct path) by editing jars or classes at fault
This works for me: close the project then re-open it, this will force eclipse to reload a fresh project and detects the correct build path.
None of the other answers worked for me. Even after fixing my build path issues, doing a refresh, clean, rebuild, and restart (of both eclipse and the computer), I was still getting the little red exclamation point.
I fixed it by closing the project (right-click, close project) and reopening it (double-click the closed project), which seemed to force eclipse to "notice" that the build path problems had been corrected.
For my mac osx Eclipse, I followed following steps:
Right CLick on your project, Choose Build Path > Configure Build Path
Select Libraries Tab and delete any arbitrary library or anything else causing errors in Build Path.
Click on Add Library button, Select JRE System Library and click Next.
Choose last Radiobutton option Workspace default JRE and click Finish.
Clean and build your project.
get a cmd and run
mvn eclipse:eclipse
This is what worked for me:
Go to ~/.m2/repository and deleted everything there by running rm
-rf. Make sure everything is deleted.
Re-launch eclipse or STS
Right click on project -> Maven -> update project.
On my Mac this is what worked for me
Project > Clean (errors and warnings will remain or increase after this)
Close Eclipse
Reopen Eclipse (errors show momentarily and then disappear, warnings remain)
You are good to go and can now run your project
I've seen this problem a few times (got it again right now on my home computer with Eclipse 4.2).
Forcing a resave (by changing the classpath e.g. by adding a library, save, change-back) works temporarily. But the problem comes back when Eclipse is restarted :(
Nuking the Eclipse workspace from orbit will permanently fix it (until next time). But there must be a better way!
just check if any unnecessary Jars are added in your library or not. if yes, then simply remove that jars from your library and clean your project once. Its worked for me.
In Eclipse this worked for me: right click project. -> Properties -> Library Section; Add (any library at all) -> select library and click remove -> press okay.
I ran into this annoying issue with the Play framework. It would be nice if there was some way of knowing what build errors Eclipse is unhappy about, but it's not going to tell you. With one project, I was able to close the project, rebuild the Eclipse configuration with sbt eclipse, and reopen. With an almost identical project, that didn't work. But deleting the project, rebuilding the Eclipse configuration with sbt eclipse, and importing, did the trick.
If you think you've done everything correctly but Eclipse still complains about the jars, refresh the folder where the jars are and make sure eclipse knows they've been added to the project. Specifying the file path alone is (apparently) not enough
I've faced this issue a couple of times and following the below steps has resolved both the times.
1. Navigate to C:\Users\
2. locate the ".m2" folder and delete it.
Now navigate to the particular project in eclipse and Right-click on the project > Maven > Update Project
wait until the project is updated and in my case following the above steps resolved both the times.
I was getting an additional warning
The compiler compliance specified is 1.6 but a JRE 1.8 is used
Resolving this warning make the error also go away. The steps are as follows:
I right-clicked on it, then clicked on Quick Fix. From the dialog that opened I selected Open the Compiler Compliance property page, and clicked the Finish button.
(This is same as Java Compiler section.)
In this dialog I found the Compiler compliance level drop down and changed 1.6 to 1.8, and clicked on Apply and close.
I got a message box Compiler Settings Changed which asked if I wanted to Build the project now?. I clicked on Yes.
The build path error went away.
Go to > Right CLick on your project folder > Build Path > Configure Build Path > Libraries Tab > remove project and external dependencies > apply & close
Now, Gradle refresh your project.
Added below to pom.xml file and it worked eventually:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
Goto to Project=>Build Automatically . Make sure it is ticked

Null Pointer Importing Gradle Project Into Eclipse

I used libGDX to generate some gradle projects that I now need to import into Eclipse. So, I opened Eclipse, installed the gradle plugin, and went to File -> Import -> Gradle Project.
At that point, I chose the directory that contained my generated gradle projects. Then, I clicked 'Build Model' and then added my projects to the list of available projects to import. After I select 'Finish' Eclipse begins importing them. Right at the very end I get this dialog:
Looking in the log file, I found this stack trace:
java.lang.NullPointerException
at org.springsource.ide.eclipse.gradle.core.wizards.GradleImportOperation.refreshProjects(GradleImportOperation.java:256)
at org.springsource.ide.eclipse.gradle.core.wizards.GradleImportOperation.perform(GradleImportOperation.java:195)
at org.springsource.ide.eclipse.gradle.ui.wizards.GradleImportWizard$1.doit(GradleImportWizard.java:66)
at org.springsource.ide.eclipse.gradle.core.util.GradleRunnable$1.run(GradleRunnable.java:49)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
I've tried reinstalling everything relevant. I've created the projects several times. I've downloaded a fresh version of eclipse. But, the same issue occurs everytime. Any help would be great. Thank you.
In case it helps someone else in the future, I had the same problem, but a different resolution. My issue was resolved when I moved the projects out of the eclipse workspace directory. It can be in another directory entirely, or in a subdirectory.
BROKEN:
/path/to/workspace/.metadata
/path/to/workspace/build.gradle
/path/to/workspace/core
/path/to/workspace/etc
WORKS:
/path/to/workspace/.metadata
/path/to/workspace/projects/build.gradle
/path/to/workspace/projects/core
/path/to/workspace/projects/etc
I did not need to remove the "afterEclipseImport" script. See this issue: https://github.com/libgdx/libgdx/issues/1537#issuecomment-38535167
As it turns out, when importing a gradle project, there is a field called "Run After" which defaults to "afterEclipseImport". This doesn't exist unless it is defined in the project you are importing. The documentation says that if it doesn't exist then the gradle import project skips over it. Well, once I unchecked this option, the import worked correctly.
Edit: Just as an aside. This assumes that your project is in a subdirectory inside your workspace as opposed to the workspace directory itself. (See the other answer).
I had a similar problem with eclipse 2019-03 and gradle 5.3.1.
In the end I created a new workspace and I was then able to import the gradle project.
Originally I had a workspace and tried to create a second gradle project from within eclipse but it failed trying to synchronise with gradel.
I also tried moving the project out of the workspace dir but it still refused to import.

Error when importing an existing project to eclipse

So I'm trying to import an existing project to eclipse and when I do I get the following error. The main folder of the project in the package explorer has a red exclamation mark (!) next to it and console says the same error 3 times:
[2013-07-10 14:23:39 - Myproject1] Project has no target set. Edit the project properties to set one.
[2013-07-10 14:23:39 - Myproject1] Project has no target set. Edit the project properties to set one.
[2013-07-10 14:23:39 - Myproject1] Project has no target set. Edit the project properties to set one.
I did my research on this error and found a bunch of stuff on this website already but nothing helps. People are mostly getting this error when they try to execute the imported project, not as soon as they import it. I also tried setting the "target set" in the project properties, as the error tells me to, but I can't find where to set the "target set" anywhere in the properties.
One more thing worth mentioning also, if I open the src folder and try to open any of the *.java, everything is blank, the "code" opens but it's blank. Any help would be greatly appreciated!
The file 'project.properties' is missing. It must reside in the root-folder of your project.
( Where the AndroidManifest.xml is located )
You should create a new android-project and import the sources and resources
OR create it manually.
Example content for project.properties:
# Project target.
target=android-17
Greetings
Christopher
set android target version on your project manifest xml file or eclipse ->right click project click properties->select android tab->check latest android version on the list like below screen shot:
after click ok button will apply wait some seconds after your issue will be solved!
you should try fixing project properties:
rightClick on Project -> Android Tools -> Fix Project Properties.
That resolved my issue!
You are probably importing your project folder on your workspace. In my case, i got that same error, so i just move the project folder to "C:" and tryed to import again. And bingo!

Error in Eclipse: "The project cannot be built until build path errors are resolved"

I am a computer science student learning Java, so I do some work at home and at college on a mixture of Linux and Windows. I have a problem after copying a new project into the Eclipse workspace. The project shows up, but with a red exclamation mark and an error saying:
The project cannot be built until build path errors are resolved
How can I fix this? I tried the solution described here, but it didn't work.
Identify "project navigator" or "package explorer" view.
Right click on your project, select Build Path --> Configure build Path.
In the emerging window, you will find four tabs, select "Libraries".There, under "Web app libraries" (expand it), you will see the libraries added to the project's classpath. Check if all of them are available. If one or more are not (they'll have "missing" beside their name and a red mark on their icon), check if you need them (perhaps you don't); if you don't need them, remove it, if you need them, exit this window, look out for the missing jar and IMPORT it into your project.
Open the Problems view. You can open this view by clicking on the small + sign at the left hand bottom corner of eclipse. It's a very tiny plus with a rectangle around it. Click on it and select problems.
The problem view will show you the problems that need to be resolved.
If the message says "the project is missing the required libraries...", you need to configure your build path by right clicking on your project, selecting properties, then build path. Add the required jar files using the libraries tab.
-If there are other problems other than missing libraries, you need to post the exact problems here to get a precise solution.
If you can't find the build path error, sometimes menu Project → Clean... works like a charm.
In my case, all libraries in the build path were OK.
To solve it, I deleted all project metadata (.project, .classpath, .settings) and re-imported the project as a Maven project.
Right click your Project > Properties > Java Build Path > Libraries
Remove the file with red "X" (something like JRE...)
Add Library
That's how I solved my problem.
I also had this problem in my system, but after looking inside the project I saw the XML structure of the .classpath file in the project path was incorrect. After amending that file the problem was solved.
If not working in any case...then delete your project from the Eclipse workspace and again import as a Maven project if that is a Maven project. Else import as an existing project.
I tried all the previous given solutions, but they didn't work, but it works for me.
In Eclipse, go to Build Path, click "Add Library", select JRE System Library, click "Next", select option "Workspace default JRE(i)", and click "Finish".
This worked for me.
Go to Project > Properties > Java Compiler > Building
Look under Build Path Problems
Un-check "Abort build when build path error occurs"
It won't solve all your errors but at least it will let you run your program :)

Categories

Resources