Getting "cannot find Symbol" in Java project in IntelliJ

JavaIntellij Idea

Java Problem Overview


I make this call to a static singleton instance from the class GameManager.java.

HUD.getInstance().update(timeDelta);

HUD.java contains the HUD class as well as two other related classes, HUDTextElement and HUDElement. All the classes are in the same root path ../src/org/mypackage.

However, when compiling this project in IntelliJ I get cannot find Symbol HUD on the line I make the HUD.getInstance() call.

This exact same code compiles just fine in Eclipse, any idea what the problem is?

Java Solutions


Solution 1 - Java

Select Build->Rebuild Project will solve it

Solution 2 - Java

I had the same problem and fixed it by clicking File>Invalidate caches/ restart

Solution 3 - Java

I had the same problem, and turns out I had never completely compiled the fresh project. So right-clicking and selecting Compile'' (shift-cmd-F9 on mac) fixed it. It seems the compile on save does not 'see' non-compiled files.

Marking the src folder as source did not help in my case.

Solution 4 - Java

This is likely to be your ../src folder is not marked as a "source" folder in Intellij IDEA, so it doesn't know to look there to find your class. You can right click the folder in the project explorer and choose "mark as source folder" to fix this.

Solution 5 - Java

I was getting the same "cannot find symbol" error when I did Build -> Make Project. I fixed this by deleting my Maven /target folder, right clicking my project module and doing Maven -> Reimport, and doing Build -> Rebuild Project. This was on IntelliJ Idea 13.1.5.

It turns out the Maven -> Reimport was key, since the problem resurfaced a few times before I finally did that.

Solution 6 - Java

For me - I tried these steps(Invalidate Cache & Restart, Maven Reimport)) but they didn't work. So I deleted the .idea, .settings, and .project folder and tried - it worked.

Solution 7 - Java

Thanks for the help so far, turns out the fix was to compile HUD.java first (right click on the file-> Compile HUD.java). After compiling the java file the rest of the project could be compiled without any problems.

I don't really know why this fixed it, or why IntelliJ wouldn't do this automatically, but root error seems it has to do with IntelliJ not correctly handling having multiple classes in a single .java file.

Solution 8 - Java

I faced the same problem, and there are a lot of solutions given in the answer, trying all these solutions took me quite some time, so here I would like to propose a methodical approach if you get this error.

Check for the following things, create/update if something is missing

  1. src folder is marked as the source folder
  2. .imls files are present
  3. Annotation processing is enabled
  4. If you recently used @UtilityClass then it can also be the reason, Bug Link

If everything is fine, you can try following solutions in given order

  1. Recompile the file/module

  2. If that didn't fix the issue, try refreshing maven dependency and building the project using Maven -> Reimport and Build -> Rebuild Project

  3. Try mvn clean install -DskipTests

  4. Try invalidating the IntelliJ cache and restarting the IDE, using File > Invalidate caches/ restart

  5. Delete the .idea folder and reimport the project

Credit and Thanks to everyone who answered this question, you can refer to their answers for more description regarding each point.

Solution 9 - Java

This happened to me when I deleted a folder and then copy-pasted it back to the project.

Right-click project folder -> Rebuild worked for me.

Solution 10 - Java

I had to right-click the project, and select "Reimport" under the "Run Maven" submenu.

Solution 11 - Java

I use maven in my project. For some reason IntelliJ was giving me these kind of wierd errors. I ran mvn clean and tried a resync and these errors disappeared.

Solution 12 - Java

I solved this problem doing right click on Java Folder and Rebuild

IntelliJ screenshot

Solution 13 - Java

recompiling the main Application.java class did it for me, right click on class > Recompile

Solution 14 - Java

If you are using Lombok, make sure you have enabled annotation processing.

Solution 15 - Java

For me was a problem with Lombok, because it requires Annotation Processing to be enabled. You can find this checkbox on Settings > Build > Compiler > Annotation Processors

Solution 16 - Java

I know this is an old question, but as per my recent experience, this happens because the build resources are either deleted or Idea cannot recognize them as the source.

Wherever the error appears, provide sources for the folder/directory and this error must be resolved.

Sometimes even when we assign sources for the whole folder, individual classes might still be unavailable. For novice users simple solution is to import a fresh copy and build the application again to be good to go.

It is advisable to do a clean install after this.

Solution 17 - Java

I know this thread is old but, another solution was to run

$ mvn clean install -Dmaven.test.skip=true

And on IntelliJ do CMD + Shift + A (mac os) -> type "Reimport all Maven projects".

If that doesn't work, try forcing maven dependencies to be re-downloaded

$ mvn clean -U install -Dmaven.test.skip=true

Solution 18 - Java

For my case, the issue was with using Lombok's experimental feature @UtilityClass in my java project in Intellij Idea, to annotate a class methods as "static". When I explicitly made each method of the class as "static" instead of using the annotation, all the compilation issues disappeared.

Solution 19 - Java

For me, the error was coming from @RequiredArgsConstructor(onConstructor = @__(@Inject)), and the message was cannot find symbol __. The error message right above this was "java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled".

Adding argument below in VM options worked as suggested here worked for me.

-Djps.track.ap.dependencies=false

Solution 20 - Java

Since this is the first hit on Google searching for "intelliJ cannot find symbol" error, I'm gonna throw in my solution as well.

The problem for me was that my project originated from Eclipse, and some files contained dependency on classes that were generated in src/generated-sources by specifications in pom.xml. For some reason, this was not properly executed when I first opened the project and rebuilding/re-importing did not help, so the files were never generated.

The solution was to right-click on the module, and select Maven -> Generate Sources and Update Folders That solved the issue and I could compile.

Solution 21 - Java

Make sure the source file of the java class you are trying to refer to has a .java extension. It was .aj in my case (I must have hit "Create aspect" instead of "Create class" when creating it). IntelliJ shows the same icon for this file as for "normal" class, but compiler does not see it when building.

Changing .aj to .java fixed it in my case.

Solution 22 - Java

Sometimes the class you want is in the test source directory. Happened to me, anyway…

Solution 23 - Java

I was having the same problem except that I was importing the classes for which dependencies were not resolving somehow. I refreshed maven projects, Rebuild Project. It still did not resolve. Looks like IntelliJ was caching something incorrectly. I restarted IntelliJ and that resolved the dependencies. I guess it cleared the cache somehow.

Solution 24 - Java

I'm seeing a lot of answers proposing a build or a re-build but just in case this don't fix your problem just notice that IDEA can detect a method but it will not compile in case you have a new before as it will be expecting the instance.

enter image description here

Solution 25 - Java

In my case, I had a problem with finding a class from another module. In pom.xml, I just had this dependency with <scope>compile</scope> specified. Removing this line helped.

Solution 26 - Java

This works for me, say class A depends on class B(and class c, d etc) but the error throws on class A which does not have any errors. So I try to compile class A alone first ->it shows error on the package of class B. So tried to compile whole package of class B. Now it throws which is the exact error class(on my case class B had error). Usually Intellj shows the exact error class with line number when compile/build. On some occasions it throws error in wrong place/class. Have a try.

Solution 27 - Java

Once when Maven Reimport, Rebuild Project, Invalidate caches didn't help I deleted subfolders artifacts and libraries from .idea folder, not whole one, so I saved custom project settings.

...and when nothing written here helps check idea.log file as posted here

Solution 28 - Java

I tried everything and nothing worked for me. So, after wasting a couple of hours, I decided to upgrade the IntelliJ idea version, and finally, it worked!

Solution 29 - Java

Delete all the contents inside ~/.m2 . Start intellij-idea. (Using IntelliJ IDEA 2021.3 (Community Edition))

Solution 30 - Java

I know this is old, but for anyone else, make sure that the class that's missing is in the same package as the class where you get the error/where your calling it from.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoeView Question on Stackoverflow
Solution 1 - JavatibooView Answer on Stackoverflow
Solution 2 - Javaganninu93View Answer on Stackoverflow
Solution 3 - JavaSjoerd KView Answer on Stackoverflow
Solution 4 - JavaPaul SanwaldView Answer on Stackoverflow
Solution 5 - JavaChristian WilkieView Answer on Stackoverflow
Solution 6 - JavaR11GView Answer on Stackoverflow
Solution 7 - JavajoeView Answer on Stackoverflow
Solution 8 - JavaDeepak PatankarView Answer on Stackoverflow
Solution 9 - JavaTomas LukacView Answer on Stackoverflow
Solution 10 - Javabitsmcgee77View Answer on Stackoverflow
Solution 11 - JavaMoiz RajaView Answer on Stackoverflow
Solution 12 - JavaFederico TraimanView Answer on Stackoverflow
Solution 13 - JavaTaranjit KangView Answer on Stackoverflow
Solution 14 - JavaMuzammilView Answer on Stackoverflow
Solution 15 - JavaRaulDanielPopaView Answer on Stackoverflow
Solution 16 - JavaMattView Answer on Stackoverflow
Solution 17 - JavaedmarView Answer on Stackoverflow
Solution 18 - JavaRamView Answer on Stackoverflow
Solution 19 - JavalearnererView Answer on Stackoverflow
Solution 20 - JavaAraklajView Answer on Stackoverflow
Solution 21 - JavafraczView Answer on Stackoverflow
Solution 22 - JavaSam BarnumView Answer on Stackoverflow
Solution 23 - JavaMoneeKView Answer on Stackoverflow
Solution 24 - JavaCarlos López MaríView Answer on Stackoverflow
Solution 25 - JavanikiforovpizzaView Answer on Stackoverflow
Solution 26 - JavaRaja aarView Answer on Stackoverflow
Solution 27 - JavaSergei RyzhovView Answer on Stackoverflow
Solution 28 - JavaAConsumerView Answer on Stackoverflow
Solution 29 - Javadr0iView Answer on Stackoverflow
Solution 30 - JavaPasha ShestakovView Answer on Stackoverflow