168

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?

2240
  • 1,547
  • 2
  • 12
  • 30
joe
  • 2,139
  • 2
  • 15
  • 13

36 Answers36

298

Select Build->Rebuild Project will solve it

tiboo
  • 8,213
  • 6
  • 33
  • 43
  • 47
    Note for complete noobs to Java/IntelliJ: above is different from "Build Project" – hiljusti Aug 05 '18 at 05:36
  • 5
    @JustinHill - I am well versed with eclipse and new to IntelliJ. Can you help me with respects to what's the difference between the two and any points/links that help me settle my time with IntellJ. – nanosoft Feb 16 '19 at 13:06
  • 1
    I'm probably a worse resource than a google search, I haven't used Eclipse. My comment is just that "Rebuild Project" and "Build Project" are different options (I was helping someone who came across this and they didn't notice the difference between the options) – hiljusti Apr 06 '19 at 02:43
  • 7
    Why did this work? Invalidating caches and restarting didn't even work. – b15 May 29 '19 at 17:41
  • Worked.... Tried running "maven clean compile" but that was not solving the problem – Omtara Dec 08 '19 at 11:13
  • Why this answer is not showing as top answer despite having highest number of upvotes? I wasted an hour figuring this out. – Ashwani Agarwal Sep 14 '20 at 09:50
  • Exactly this is causing the issue! – wilmol Sep 22 '20 at 11:30
  • I used "build", clean cache, restarted, reload... nothing. I found this, I thought it won't do much but that's the trick! The magic word "Rebuild", depending in your settings, you may get a couple of "rebuild..." options. Thanks! – Leonardo Brambilla Oct 21 '21 at 15:30
  • LUL, this also failed with "cannot find Symbol" for me, but in different module. – RAM237 Jun 29 '22 at 09:11
  • Not worked. I have the right pom.xml but still having the same issue – Ashu May 27 '23 at 02:18
91

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

ganninu93
  • 1,551
  • 14
  • 24
  • 28
    Invalidating caches does not result in lost changes. I'm not even sure how that would work. If you save a file, it's saved, regardless of IntelliJ caching or committing to a repository. – Christopher Schneider Jan 06 '20 at 16:43
  • One of the latest answers here did it for a rather old question. – Kawu Nov 16 '20 at 16:43
  • OMG, I have Tomcat and a bunch of Spring Boot apps running, I don't want to restart :( All I need is to re-compile one single class which was recently successfully compiled and deployed and all I changed is a boolean condition... Why, Intellij, why ?! – RAM237 Jun 29 '22 at 09:16
60

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.

Sjoerd K
  • 709
  • 5
  • 2
36

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.

Christian Wilkie
  • 3,693
  • 5
  • 34
  • 49
  • Similarly you can also delete your offending module (a gradle one in my case) and then reimport with File -> New -> Module from existing sources which is what I had to do. Though I also had to modify the .idea file manually for reimport to finally work: https://stackoverflow.com/questions/18881062/intellij-12-cant-reimport-module – Frikster Jan 27 '21 at 00:07
35

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.

Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
  • 1 is a possiblity, but wouldn't 2 not give him an error saying it couldn't find HUD, but rather an error saying it doesn't have getInstance/update method? – Alex Coleman Aug 26 '12 at 16:55
  • this option seems to be gone. – clankill3r Sep 11 '13 at 15:36
  • No, it transform to popup options: "Mark Directory As >" – Dracontis Nov 29 '13 at 10:43
  • My src contain test folder inside, and multi modules, after mark it as Source Root my issue is worst. – Se Song Oct 31 '17 at 02:51
  • For me this was close to the right answer. My source root was already marked, but I had to "Unmark as Sources Root" and then mark it as the Sources root again in order to fix the error. – Lawrence Jan 03 '18 at 23:20
20

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.

Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35
  • 2
    works : Try invalidating the IntelliJ cache and restarting the IDE, using File > Invalidate caches/ restart – Energy Jul 30 '20 at 08:40
19

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.

R11G
  • 1,941
  • 8
  • 26
  • 37
  • 10
    Deleting the `.idea` folder did it for me. – Daniel D. Jan 14 '20 at 10:30
  • 1
    This worked as I had a couple of libraries on a second drive and windows update (oh the gift that keeps giving) created a boot partition that changed the drive letter. Even removing the old links and re adding them under project settings libraries didn't work until I deleted the .idea folder. Then option to recompile modules appears under the build menu. – Andrew Jul 08 '21 at 14:06
  • I deleted .idea, .build, .gradle and this solved the issue for me – jMan Oct 12 '22 at 09:11
15

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

IntelliJ screenshot

Federico Traiman
  • 1,191
  • 1
  • 13
  • 18
12

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.

joe
  • 2,139
  • 2
  • 15
  • 13
  • 1
    IntelliJ can handle multiple classes in a single file just fine, it just needs to be sure that the file was compiled at some point. – Makoto Aug 26 '12 at 18:27
  • Yeah, except that I just had the same issue with a single class file. Oddly enough, it had been running just fine right before I ran this maven command: `mvn com.zenjava:javafx-maven-plugin:2.0:fix-classpath`. It's meant to move the javafx jar from the libs directory to the ext directory for the current JDK. I found it [here](http://zenjava.com/javafx/maven/fix-classpath.html). – nihilon Mar 20 '15 at 13:32
11

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.

Tomas Lukac
  • 1,923
  • 2
  • 19
  • 37
7

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

bitsmcgee77
  • 391
  • 3
  • 10
6

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.

Moiz Raja
  • 5,612
  • 6
  • 40
  • 52
  • 1
    clean is a maven lifecycle. You use it if you want to clean up all the output files that have been created by the maven project. – Moiz Raja Sep 11 '13 at 16:17
4

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

Taranjit Kang
  • 2,510
  • 3
  • 20
  • 40
3

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

Muzammil
  • 417
  • 1
  • 4
  • 20
3

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

RaulDanielPopa
  • 1,311
  • 1
  • 8
  • 2
2

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.

Matt
  • 171
  • 3
  • 13
2

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
edmar
  • 36
  • 1
  • 5
  • In my case there were autogenerated classes (based on XML files) that other parts of the code referenced. While building with maven works, if not `mvn install`ed, IntelliJ did not see those references and failed the run-environment. – tokosh Jul 29 '19 at 06:50
2

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
learnerer
  • 396
  • 1
  • 2
  • 17
1

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.

Ram
  • 191
  • 1
  • 2
  • 12
0

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.

Nimbrod
  • 57
  • 11
0

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.

fracz
  • 20,536
  • 18
  • 103
  • 149
0

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

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
0

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.

MoneeK
  • 25
  • 6
0

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

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45
0

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.

nikiforovpizza
  • 487
  • 1
  • 7
  • 13
0

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.

Raja aar
  • 59
  • 9
0

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

0

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!

AConsumer
  • 2,461
  • 2
  • 25
  • 33
0

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

dr0i
  • 2,380
  • 2
  • 19
  • 36
0

In my case,

  1. close intelliJ.
  2. delete ".idea" (which are hidden folders by default) from project root.
  3. open IntelliJ
  4. open settings --> find annotation processors --> check "Enable annotation processing"
  5. Open Project structure from "File" menu. Set the appropriate JDK version.
coverboy
  • 51
  • 1
  • 10
0

In my case, it had nothing to do with the project build or the JDK version mismatch for the project and modules. Giving some context about what I was building:

  1. I was creating a REST Controller, and a PostMapping in that controller.
  2. Method accepts a couple of path parameters.
  3. @Valid and @Size(min, max) constraints were applied on these params.
  4. What I did wrong was that I used a non-constant type from the service constant file to pass in the value for the min and max allowed in the @Size() annotation. In the service constant I by mistake had defined min/max as the public static final Integer type instead of the **int** type. This is a compilation issue as @Size would need constant values for min and max.
  5. So, check for such silly mistakes as well. Might save you time and effort.
0

Make sure the "Target bytecode version" in the Java Compiler settings is set to a java version that contains the feature you're trying to use. This value is normally set to "Same as language level" but can be overridden for individual modules. When that happens the code editor will show warnings based on the other language settings but the compiler will generate bytecode for the specified java version. If those language settings don't match it can be difficult to diagnose the problem.

0

In my case, I accidentally added a file to the compiler exclusion list somehow. I went to Preferences -> Compiler -> Excludes, and then removed the file from the exclusion list.

0

In my case, all packages of lombok were imported I removed this code and imported each required package separately and the problem was solved.

import lombok.*; // <-- I just removed this line and reimport packages

I also don't know why this solved the problem I tried all the solutions in the comments here, but it didn't work.

Seunghoon Baek
  • 611
  • 6
  • 16
-1

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.

  • Downvoted, as classes can be imported from other packages. Generally, the target class must be imported and in classpath; when using Maven, the dependency must be set correctly. – fxnn Dec 05 '17 at 11:38
-1

For me: I ran an mvn clean install in console(not with embedded IntelliJ maven). Then when I tried again in Intellji maven, it was working fine.

SpongeBob
  • 383
  • 3
  • 16