joe
joe

Reputation: 2319

Getting "cannot find Symbol" in Java project in IntelliJ

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?

Upvotes: 184

Views: 332937

Answers (30)

blm99
blm99

Reputation: 87

I had this problem and it was resolved by adding the following highlighted block into the pom.xml file.

   <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <!-- Compiler plugin for specifying Java version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

enter image description here

Remember to update maven afterwards

enter image description here

Upvotes: 2

ReemRashwan
ReemRashwan

Reputation: 371

For me, I excluded a file in build time by mistake which caused this problem. Followed these steps to remove it from excludes:

https://stackoverflow.com/a/65753260/8963778

Upvotes: 0

Seunghoon Baek
Seunghoon Baek

Reputation: 631

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.

Upvotes: 0

Jack Thomas
Jack Thomas

Reputation: 44

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.

Upvotes: 0

littlebooboo
littlebooboo

Reputation: 25

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.

Upvotes: 1

Prashant_Sharma
Prashant_Sharma

Reputation: 176

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.

Upvotes: 0

coverboy
coverboy

Reputation: 61

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.

Upvotes: 1

SpongeBob
SpongeBob

Reputation: 464

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.

Upvotes: 1

dr0i
dr0i

Reputation: 2480

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

Upvotes: 0

Federico Traiman
Federico Traiman

Reputation: 1251

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

IntelliJ screenshot

Upvotes: 17

AConsumer
AConsumer

Reputation: 2801

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!

Upvotes: 0

Sergei Ryzhov
Sergei Ryzhov

Reputation: 51

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

Upvotes: 0

learnerer
learnerer

Reputation: 494

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

Upvotes: 2

Raja aar
Raja aar

Reputation: 69

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.

Upvotes: 0

Deepak Patankar
Deepak Patankar

Reputation: 3302

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.

Upvotes: 20

nikiforovpizza
nikiforovpizza

Reputation: 576

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.

Upvotes: 0

RaulDanielPopa
RaulDanielPopa

Reputation: 1599

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

Upvotes: 3

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

Upvotes: 0

Muzammil
Muzammil

Reputation: 477

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

Upvotes: 3

R11G
R11G

Reputation: 1980

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.

Upvotes: 20

Tomas Lukac
Tomas Lukac

Reputation: 2265

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.

Upvotes: 11

edmar
edmar

Reputation: 36

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

Upvotes: 2

Taranjit Kang
Taranjit Kang

Reputation: 2580

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

Upvotes: 4

Ram
Ram

Reputation: 191

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.

Upvotes: 1

bitsmcgee77
bitsmcgee77

Reputation: 391

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

Upvotes: 7

MoneeK
MoneeK

Reputation: 25

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.

Upvotes: 0

Sam Barnum
Sam Barnum

Reputation: 10744

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

Upvotes: 0

zeroboo
zeroboo

Reputation: 8993

Select Build->Rebuild Project will solve it

Upvotes: 330

fracz
fracz

Reputation: 21278

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.

Upvotes: 0

Nimbrod
Nimbrod

Reputation: 57

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.

Upvotes: 0

Related Questions