198

I completely uninstalled IntelliJ and have now reinstalled and imported my project. I imported using gradle and can see all of the files in my project present. However, when I open a file I can't find any declaration to go to by doing cmd + click. I have the Java 7 SDK setup, and I can't even go to the declaration of classes in my own project. Going to a Java core class like String or List doesn't work either. How can I enable the IDE to be able to find my classes?

I'm on OSX Yosemite, IntelliJ 2016.1.2 build 145.972.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Andy
  • 2,138
  • 3
  • 14
  • 14
  • 38
    Is your source directory marked as source route? If not, right-click on a directory with java classes and then: Mark Directory As -> Sources Root – AniaG May 17 '16 at 21:05
  • In my case I needed to mark my resources folder a `Resources Root`. – Genfood Apr 16 '19 at 14:23

35 Answers35

387

I had this same problem, and @AniaG's solution in the comments worked for me.

  • Right-click src folder
  • Mark Directory as > Sources Root

enter image description here

Thomas
  • 5,810
  • 7
  • 40
  • 48
  • 4
    There is no option "Mark directory as" in the context menu anymore. Use CMD + shift +A to find "mark directory as" action – Leo DroidCoder May 22 '17 at 09:17
  • 3
    I still see it as an option in IntelliJ IDEA 2017.1.3 – Thomas May 22 '17 at 18:44
  • 17
    Mark the `java` folder instead of `src` works for me – Searene Jan 10 '18 at 02:29
  • 5
    When I set this, errors show the whole project. What's wrong. – Priyantha Nov 28 '18 at 06:14
  • The above solution worked for me once. But next time it did not. When i started Intellij on our pom project. It showed one warning, something like "unmanaged pom file found". I clicked on it and chose to link it and it worked after that. – JavaTec Apr 15 '19 at 16:55
  • 3
    With a Maven directory structure, I had to mark src/main/java as Sources Root, src/main/resources as Resources Root, and the test directories as the test sources counterparts. I always need to control my "fist of death" when some n00b tells me in a condescending manner that "developing with Intellij is much faster than with Eclipse." – Steve Gelman Apr 29 '19 at 13:38
  • Attention! This will destroy setups from Intellij framework support. In my case, the Spring context has been deleted within Intellij and I had to rebuild it in "Project Structure" - "Modules". – Jochen Haßfurter Sep 05 '19 at 10:16
  • In my case java directory was already marked, I just needed to move my classes there (because I added maven support after creating and placing the classes). – Line May 01 '20 at 19:18
  • This is not solving any problem. I even see Kotlin not configured error in android studio. Can you give explanation as to what this is going to do and how it is solving the problem please? – The_Martian Oct 13 '20 at 14:44
  • 1
    This was the only worked for me. But why do we need to do this? What mistake happened that weone needs to do this? – Abdul Waheed Jan 20 '21 at 07:36
  • In my case, the following step worked, right-click pom.xml and Select Maven -> Reload project – Himanshu Aggarwal Jul 23 '21 at 19:51
80

I faced the same issue and spent almost 15-16 tiring hours to clean, rebuild, invalidate-cache, upgrade Idea from 16.3 to 17.2, all in vain. We have a Maven managed project and the build used to be successful but just couldn't navigate between declaration/implementations as Idea couldn't see the files.

After endlessly trying to fix this, it finally dawned to me that it's the IDEA settings causing all the headache. This is what I did (Windows system):

  1. Exit IDE
  2. Recursively delete all .iml files from project directory del /s /q "C:\Dev\trunk\*.iml"
  3. Find and delete all .idea folders
  4. Delete contents of the caches, index, and LocalHistory folders under <user_home>\.IntelliJIdea2017.2\system
  5. Open Idea and import project ....

VOILAAAAAAAAAAAA...!! I hope this helps a poor soul in pain

DennisLi
  • 3,915
  • 6
  • 30
  • 66
user8382868
  • 801
  • 6
  • 4
  • 3
    Where is .IntelliJIdea2017.2\system? Is that path system specific? – Daniel Reina Feb 19 '18 at 12:26
  • 1
    I was experiencing this for `build.gradle.kts` files, and deleting the `.idea` directory and .`iml` project file was sufficient enough to fix the issues. Of note: this was opening a Linux project in Windows (both versions of IntelliJ were the same - 2019.3) – Nathan Jan 04 '20 at 15:01
  • Clearing the .iml-Files just in that sub-module where IntelliJ could not find the declarations was working for me. It is not necessary to clear out the .idea folder (which also contains your runConfigurations). – ShadowGames Sep 18 '20 at 10:52
  • worked on linux, thanks; obv. rm -Rf – HellishHeat Feb 13 '22 at 16:47
47

I had the same issue with idea and I was trying to open a maven project but the pom files where not identified. So right clicking on the pom file and choosing "add as maven project" did all the magic for me :)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Pavithra
  • 471
  • 4
  • 2
  • 1
    likewise but for me it was "reimport" – jsharp Aug 07 '19 at 16:10
  • 2
    Same! Just after "add as maven project" I had to again right click pom.xml -> Maven -> Reload Project. and it started navigating :) – QauseenMZ Jun 05 '20 at 20:03
  • I did have the add as maven project option, not sure why :( – Indrajeet Gour Jan 04 '22 at 10:07
  • Same for me. Right now the command can be found under File / New / Projecr From Existing Sources... , choose the pom file. After that I clicked on Reload All Maven Projects button in Maven view. – user07 Jan 19 '23 at 20:16
36

Had the same problem. But only on my own methods.
Just fixed it by invalidating cache: (File-> Invalidate Caches/Restart)

notihs
  • 654
  • 7
  • 19
18

Most of the times there has been a problem with building the cache of the IDE, or something related.

Most probably, File -> Invalidate Caches / Restart, will resolve this problem, just let the IDE finalize it's caching.

SpiXel
  • 4,338
  • 1
  • 29
  • 45
  • 1
    This has always worked for me. It should definitely be used before some of the more dramatic procedures above. First right click project->maven reload project, then this, finally look at some the other procedures outlined above – mancocapac Apr 23 '21 at 11:37
  • This worked for me. I am working Rust workspace. – Peter May 08 '23 at 10:55
18

I was having similar issues in my IntelliJ mvn project. Pom.xml was not recognized. What worked for me was right click on the pom.xml and then add as a maven project.

Addy
  • 191
  • 1
  • 3
8

Your source folders where your Symbols are (Classes) are need to be configured as "Content Root".

  • Open Project Structure: #;
  • Click Modules
  • You can configure your Content Root (Sources, Tests, Resources etc) for each module that you want to Navigate to.

Once done you should be able to navigate to your symbols.

ljk
  • 488
  • 1
  • 5
  • 11
sujeet
  • 91
  • 1
  • 5
  • for some reason ALL the subfolders of the root folder of the project were marked as excluded. thanks @sujeet – iamveritas Mar 31 '22 at 16:56
7

If you see red circle on the icon of Java files, and got warning like "cannot find declaration to go to". Then you can do something below:

  1. Right click root folder of your project/module.
  2. Mark Directory As -> Excluded.

enter image description here

It works for me. in Windows 7, Intellij 2016.1.1

Community
  • 1
  • 1
Vic.Liu
  • 461
  • 5
  • 3
7

Right-click on your src folder and choose the option "mark directory as" --"Source root" and along with it again right-click on your project and choose "reformat code"(under this choose cleanup code too) and that will work fine.

Aman Agrawal
  • 71
  • 1
  • 2
6

In my case, I just updated my IntelliJ to Ultimate 2018.2 and all of my projects suddenly cannot find the implementations and the 'src' folders - it turned out IntelliJ removed the type of project (e.g. Maven).

What I did is:

Right click on the root project > Add Framework support... > Look for Maven (in my case) > Wait to re-index again > Then it worked again.


UPDATE 2:

I have always been encountering this when I update IntelliJ (2019.1.1 Ultimate Edition). Just click the refresh button of Maven Tab and it should re-index your current project as Maven Project:
enter image description here

pvma
  • 403
  • 8
  • 14
6

I got this problem on Big Sur. Solution: File -> Invalidate Caches. After that press Option + Enter on every red field.

4

None of the methods listed in this channel works for me. However, I am able to resolve it using the following method, and I hope that it works for you too.

Right Click the pom.xml file and then select "Maven". After that, click "Import Maven Project"
buqing
  • 925
  • 8
  • 25
3

I found this cannot find declaration to go to problem once in my Maven project. The reason for this was just that one of the sub-projects in my project did not import as maven project correctly.

Hema
  • 988
  • 1
  • 16
  • 38
Figo Wang
  • 31
  • 3
3

enter image description here

Importing my project (which had 3 different modules in it) - as File -> Module From Existing Sources fixed it for me.

R11G
  • 1,941
  • 8
  • 26
  • 37
2

Ensure that the directory which contains your compiled classes and libraries is marked as sources Root.

Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45
Anand
  • 91
  • 6
2

I too faced this issue. I've tried the solutions mentioned here. The issue seems not with the source folder.

For me the issue occurred when I installed a new version of IntelliJ, was using 2019 version moved to 2020 version. The project got opened in the new version but the declarations were missing.

I fixed this by :

 - File>Project Structure.  
 - Under Project Settings go to Modules. Here you should see the different project folders, for me it was not there. 
 - Click the + button on top and click Import Module.
 - Select the root pom.xml and wait for the indexing to complete.

After the indexing is done all the declarations were working.

Vishal Vijayan
  • 308
  • 1
  • 4
  • 13
2

For Java based project, this is what worked for me :-

-Download Intellij 2020.3.4 version instead of 2021.1.1
-Add configuration, plugins.
-Configure settings, Project Structure
-Right click on the root folder -> Mark Directory as -> Sources Root
-File -> Invalidate caches/restart -> Invalidate and Restart
2

Just run below commands

mvn idea:clean

mvn idea:idea
R.Wedisa
  • 99
  • 8
1

IDEA may ignore some of your maven dependency files. The "External Libraries" node in your project structure might be empty or incomplete.

Go to:

  1. IntelliJ --> Preferences
  2. Search for Maven [on the left tab] -> Ignored Files
  3. See if there is any path on the right side that is checked(ignored) and uncheck that.enter image description here
jediz
  • 4,459
  • 5
  • 36
  • 41
mykey
  • 1,943
  • 19
  • 13
1

Came across the same issue and in my case (Java project), I had to include all the dependent jars in the project's libraries section.

File -> Project Structure -> Libraries

I had to add my project dependent jars in the above section (for example; project/web/lib/). After doing so, all resolved fine. I hope this will help someone.

Ricky Boy
  • 723
  • 7
  • 7
1

For someone whom the above solution didn't worked.

For gradle,

File -> Project Structure

Click on Project Under Project Settings

Update Project Language level as Modules, private methods in interfaces etc.

Click OK and Import Gradle Settings

Project Structure

Ali Akbar
  • 370
  • 3
  • 9
1

I had similar issue with Goland JetBrain and what worked for me is similar to what @user8382868 said but I only closed the IDE, renamed the folder .idea file to .idea.old and after re-opening the project everything worked.

E235
  • 11,560
  • 24
  • 91
  • 141
0

In my case I was using a gradle project with subprojects. The reason IntelliJ couldn't find declarations was that the subprojects were not built.

After investigating why subprojects were not built, I found out that using "auto-import" was breaking the subprojects.

After importing the project from build.gradle without auto-import, it worked for me.

Daniel Reina
  • 5,764
  • 1
  • 37
  • 50
0

For what its worth, in Pycharm it is: Right click on the root folder->Mark Directory as-> Sources Root

mithunpaul
  • 3,268
  • 22
  • 19
0

Same Problem for Python Project!!!

I tried to make a clean new intellij project and updated everything. Tried different virtualenvs but nothing worked out.

UNTIL I uninstalled the Python Plugin + Restart IntelliJ + Intall Python Plugin + Restart IntelliJ again. I'm not sure if this is a "real" solution, but it solved my issue.

Maybe you can try something familar for Java "Plugin"/JDK?

zypro
  • 1,158
  • 3
  • 12
  • 33
0

In my case, none of the above helped. Only upgrade of Intellij from 2017.3 to 2018.2 helped.

Do not try 2018.1 as it had a bug wherein the Find files popup would not come up.

Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106
0

Somehow I set my Maven modules to be ignored. In the Maven tool window right click and select 'Unignore Projects'.

Then all the directories will be automatically selected.

enter image description here

ABC123
  • 1,037
  • 2
  • 20
  • 44
0

I had the same issue. what i did was. Select your project >right click> go to settings> plugins > search for cucumber > and then re-bundled the plugin and restart your intellij. It should fix the issue.

0

Nothing above worked for me unfortunately. I deleted all downloaded dependencies from .m2\repository and did a mvn clean install to download them again. (Although i did not have any issues in the pom.xml at the first place)

That worked for me.

0

In my case, I've to open preferences:

enter image description here

And I went to Project -> Project Structure -> set my folder project in +Add Content Root

enter image description here

So now all it work fine!

0

Python project

Along with setting the correct interpreter through the following settings path

File > Settings > Project > Python Interpreter

I also updated the project structure's content root to be the srcdirectory

File > Settings > Project > Project Structure   
- remove current content root     
- add content root to point at src
jsibs
  • 666
  • 2
  • 7
  • 25
0

Using Kotlin/Multiplatform library

Project structure > Modules Then remove [main] and [test] if platform are already used

Exemple Example of final result

0

The same issues was on Android Studio Bumblebee in XML layout file. Switching (checkout) to another branch on git and coming back to the original branch solved the issue.

Sergio
  • 27,326
  • 8
  • 128
  • 149
0

The answers provided here helped some, but not completely. This is because, for a multi-project solution where classes import classes from other projects, you will need to mark all relevant projects as Sources Root. Marking the solution folder as Sources Root will not work.

Also, I do recommend invalidating all of the caches and re-opening the solution.

J Weezy
  • 3,507
  • 3
  • 32
  • 88
-3

If you are new to IntelliJ u May take some time to understand IntelliJ. If a Code has written in Java and using cucumber framework then we have two main files -

  1. Feature file
  2. Step definition file

Goal : what are things we have defined in feature file it must be navigate towards Step Definition File. (Ctrl +click ) Note :feature file has to contain some colourful gherkins keywords too .if it’s not follow the below steps

Solution : go to file >>settings >> plugins - search “Cucumber for Java” Add the plugin restart IntelliJ it will work for sure .