Java file outside of source root intelliJ
Asked Answered
E

35

102

I have cloned a spring boot project from GitLab having multiple branches in IntelliJ. I checkout to the branch in which I want to work. But I am getting "Java file outside of source root" on all the files. Please suggest what I am doing wrong. I just installed IntelliJ and it's the first project that I am importing.

Epi answered 21/8, 2020 at 10:34 Comment(6)
What build system is used? Gradle? Maven? SBT? Make sure you've enabled the corresponding plugin in your IDE.Fermat
@JoachimSauer I am using Maven. I tried to build a project and it is building successfully. But still facing the same problem. I just installed IntelliJ and it's the first project that I am importingEpi
IntelliJ has a Maven plugin installed by default and (I think) enabled as well, so if you just import the project it should detect all the relevant source paths, unless the maven build does some unusual things ... Check this document as you didn't give us a lot of information on how you imported the project.Fermat
@JoachimSauer I imported the Gitlab project by selecting the option "import from git" and pasting the repo link.Epi
@Epi The error message "file outside of source root" hints, that your source root (and/or the files location) may be wrong. The source root should be the java folder within src folder. Right click on java folder and choose [mark directory as] > sources root. If that not helps can you pls provide us the github link and branch you worked or a minimal simple working example. Also pls provide us the complete error with stacktrace info.Galosh
This worked for me: right-click pom.xml > open maven menu > reload projectShig
C
144

If you do an 'import from git', IntelliJ doesn't import the project structure from maven (or gradle) automatically.

One way to do this afterwards is to right-click on the pom.xml file in the root directory, and select 'Add as maven project'. Then the folders will be marked correctly, and dependent libraries will be imported. There should be no need to mark individual source folders manually.

I prefer to do a git clone outside of IntelliJ, and afterwards open the project in IntelliJ from the local filesystem, then IntelliJ imports the maven project structure automatically.

Couvade answered 23/9, 2020 at 8:7 Comment(1)
I cannot find 'Add as maven project' on right clicking the pom.xml file.Carrol
D
90

A very simple solution: -

Click on "maven" on the right toolbar in IntelliJ and then click on the refresh button.

Update: Adding a screenshot on the New UI, but the location is the same on the old UI.

Toolbar location

Disburden answered 13/10, 2020 at 17:37 Comment(7)
This solution fits good for files from "target - generated-sources". Thanks!Longobard
perfect..short and sweet - thank you.Plassey
works as well for Gradle projectGladiatorial
After the maven refresh, I had to 'Invalidate Caches and Restart' Intellij IDEA.Glory
What's the right toolbar? Screenshot?Cordle
@Cordle Check the updated answer with the screenshot. You need to click on the reload button that has the tooltip saying "Reload all Maven Projects". As long as a proper maven installation is present, IntelliJ should be able to automatically resolve the required dependencies.Disburden
I think this should be the actual solution as it was simple and fast to fix the issue.Harr
G
68

I fixed it using this way:

File > Project Structure > Modules > Sources

and then marked java files dir as Sources.

Gastropod answered 21/11, 2020 at 12:17 Comment(0)
E
36

This has been driving me quite mad and I am a newbie as well. I finally solved it by using:

file > project structure >

Then going to Modules and removing all the files from the root using the x marks on the far right. I then used the +Add Content Root to reselect the src folders that contained my .java and hit Apply. Finally InteliJ sees the files as in the project and the folders gain the blue square on the main view.

Enlist answered 23/9, 2020 at 7:46 Comment(1)
Sad that I can only upvote and not give an award like on Reddit, thank you sooo muchIndirection
P
34

To Solve: "Java file outside of source root"

  1. Open Project Structure.
  2. Select Modules in Project Settings in left pane.
  3. Click on the folder in which your Java files (showing file outside of source root) are present.
  4. Then Select Sources from Mark as: option.
  5. Apply the same steps for multiple folders.

Image showing the mark as Source option

Pastiche answered 26/1, 2021 at 17:19 Comment(4)
that's the correct answer, I've upvoted itGeneticist
Took me a while to figure out where to "mark as source". You have to right click on the folder first to see that option.Ramage
I don't see "Sources" option.Roobbie
I have added image in answer showing 'Sources' option or you can select the same by right clicking on the folder to be added as source.Pastiche
A
9

MultiModule Project.

Select root pom.xml --> Click Maven -- Unlink <-- This will remove all sub modules

Select root pom.xml --> Add As Maven project

Then everythings worked..

Archdeaconry answered 14/4, 2022 at 8:35 Comment(2)
I just ran into this after recent IntelliJ update – this behavior appeared for an existing project.Tyrannosaur
I had a project which was a single module. We made the change to multi module and the merge totally confused intellij. This fixed it and basically everytime someone does a change like that, "this is the way".Edy
J
7

Right click on pom.xm >> maven >> Reload project

Jane answered 4/7, 2021 at 17:2 Comment(2)
I don't see "maven" menu item.Roobbie
Helped me, thx!Varus
P
6

Right click on pom.xml then click on "maven" and reload project enter image description here

Psia answered 2/2, 2023 at 12:40 Comment(0)
C
5

Resolved !! Go to your project on IntelliJ -> Right click on pom.xml -> Add as Maven Project.

Contractile answered 26/5, 2022 at 14:39 Comment(0)
C
4

For ppl like me who also see that the POM file is crossed out and greyed(ignored):

You can right click the POM.xml, select "Maven - Unignore projects".

Cnemis answered 29/4, 2021 at 9:36 Comment(1)
Works as magic. Give this answer way more credit please!Spreadeagle
O
3

In intellij: click Maven, select the project and right, select Unignore Projects

Odyl answered 23/9, 2021 at 20:16 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Maillot
I tried all of the above answers..none worked. Finally this answer resolved my issue. ThanksInfracostal
S
3

Solution to "Java file outside of source root intelliJ"

Regenerate Maven Sources and Update Folders

Right click the pom.xml > Maven > Generate Sources and Update Folders

The project should now run

Here's an image of what the menu looks like:

enter image description here

Saddleback answered 30/5, 2022 at 14:19 Comment(0)
P
3

File -> Project structure -> Modules

Remove the whole module, and click the add button then select 'Import Module', choose the root maven pom.xml of your project.

This saved my time.

Pilate answered 17/10, 2022 at 2:22 Comment(1)
Thanks a ton...you don't know how many hours you saved :)Seigniorage
G
2

I did a 'Maven reload' (or Gradle reload) from the upper-right 'Maven' ('Gradle') window and problem is solved.

Greensboro answered 29/4, 2022 at 13:36 Comment(0)
C
2

File ->Project Structure -> Module select the Sources tab in the third pane from the left

Carotid answered 2/5, 2022 at 15:16 Comment(0)
J
2

The solution that worked for me was to right click on the src folder and select "Mark Directory as" -> "Sources Root" or "Test Sources Root".

This is also mentioned in the comment to the question. But I had missed it so wanted to post it as an answer.

Jamey answered 4/11, 2022 at 9:4 Comment(0)
H
2

I had the same problem in my multi-module maven project. Doing some steps mentioned under this question solved the issues but it was not permanent. The issue appeared everyday. In-order to fix this I had to go to the project settings as shown below.

enter image description here

After that, I did check the following check boxes.

enter image description here

I don't know whether this would solve the issue for every occurrence. However, for me this was the fix.

  • Maven version - 3.9.1
  • IntelliJ version - 2023.1.2 (Community Edition)

I am sharing this thinking this might help someone. Thank you!

Hyperaesthesia answered 30/5, 2023 at 9:26 Comment(0)
C
1

Go to

File >>Project Structure>>Modules>> Import Module>>  (At this point your src folder will appear on the right most pane automatically ) >> Apply >> OK
Camerlengo answered 24/2, 2021 at 11:54 Comment(0)
G
1

IntelliJ may be ignoring files from a multi-module project. Modules that are ignored will make it harder for the IDE to find the root of that file. Hence, java file outside of source root

Another solution:

Preferences -> Build, Execution, Deployment -> Build Tools -> Maven -> Ignored Files

Now, uncheck the modules you do not want IntelliJ to ignore.

Gisborne answered 16/2, 2022 at 18:37 Comment(0)
W
1

I'm having a similar issue for an Android project in Android Studio Chipmunk 2021.2.1 Patch 1. It looks like a known bug related to using manifest.srcFile: https://issuetracker.google.com/issues/218370114?hl=ko

Apparently the options are switching to the canary channel or waiting for the fix to be released on the stable channel. Or getting rid of manifest.srcFile in your build.gradle.

What answered 13/6, 2022 at 16:47 Comment(0)
I
1

Go to Intelij

File > Project Structure > Modules > Add

add module

Instigate answered 17/10, 2022 at 2:50 Comment(0)
L
1

In my case, I have ignored some projects and hence the pom.xml files also showed disabled. Hence I followed the below steps.

right-clicked those pom.xml files -> Maven -> Unignore Projects

Limpkin answered 25/5, 2023 at 10:50 Comment(0)
A
1

I tried all of the above. Finally figured out this is because of an issue with incompatible Maven version. Fixing the settings in IntelliJ to refer to a lower version of Maven and then Add as Maven project helped.

Autobiography answered 25/5, 2023 at 17:5 Comment(0)
W
0

I'm just gonna throw my solution to this problem in here, since none of the above worked:

If there are multiple pom files in your project, on the Maven sidebar to the right, click the plus ('+') button and navigate to the other pom file that does not have the Maven ('M') icon next to it in the project structure of your project (left sidebar).

Then click 'OK' - this then loads all the Maven dependencies from the unused pom file and, for me, everything was finally fixed!

Maven sidebar > plus icon (+) > direct to other pom file > 'ok'

Warenne answered 28/10, 2021 at 10:30 Comment(0)
A
0

I had to open the module I worked on to a new window to solve that.

Arable answered 18/1, 2022 at 15:50 Comment(0)
D
0

Hate to add yet another method; but this one worked for me after trying and failing with the above methods. I had a multi-mode project. Unlinked all maven projects in Maven workspace and reloaded as maven project by clicking root poom.xml in project workspace. Not sure whether this will always work and for everyone.

Deposal answered 19/1, 2022 at 5:0 Comment(0)
H
0

I faced the same problem in one of the modules in my multi module maven project. In my InteliJ idea, I've installed Maven Helper plugin before. I did right click on the module and within that contextual menu click Run Maven and then Reimport. After doing that, my issue resolved.

Hyperaesthesia answered 18/2, 2022 at 12:22 Comment(0)
C
0

Just for anyone who has the minor mistake that I had:

My simple issue and fix was to cd into the exact file I cloned down. I accidentally shortcut opened the parent directory with idea . instead of the project I needed to work on.

Condition answered 4/1, 2023 at 17:52 Comment(0)
S
0

I had to change the Maven path in IntelliJ. Preferences-> Build, Execution, Deployment-> Build Tools-> Maven.

Using the bundled Maven 3 path solved this and the Spring Boot application started up just fine.

Scrawly answered 6/1, 2023 at 15:4 Comment(0)
B
0

Please do the below 2 steps:

Step-1: Right click on root pom.xml file Maven-> Generate Sources and update folders

Step-2: Right click on root project level pom.xml >> maven >> Reload project

Bukovina answered 25/1, 2023 at 8:5 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Maillot
U
0

For one project where build.gradle was configured for various custom build variants, I tried everything mentioned above but still it didn't work. Finally when I chose correct build variant from left panel of Android Studio, it just worked.

Unknit answered 18/3, 2023 at 6:39 Comment(0)
M
0

This is the solution that worked for me:

  1. Close the project in IntelliJ
  2. Delete the .idea folder in the project directory
  3. Reopen the project in IntelliJ
Modular answered 11/4, 2023 at 13:38 Comment(1)
This will remove your shelf!Lavinia
B
0

However there are many options already given, i tried 2-3 from above but it did not work as expected.Because, Intellij Idea keeps all workspace setting in .idea folder on root level of project and *.iml file under all sub-project to link the modules. To clean all the settings - either close IDEA or left idle for some time and from back using terminal executed below command - assume my root project is "ewfsa" -

cd ewfsa
rm -rf .idea
rm -rf **/*/*.iml
rm -f *.iml 

and then select option "reload from disk" and if IDEA closed then open IDEA. every project reloaded and all missing setting restored.

Bilander answered 12/5, 2023 at 12:18 Comment(0)
D
0

My new experience with this problem from September 2023 - the issue was:

  • java version path / changed java on system

I changed system Java from Java 17 to 1.8 and after that, I opened Inteliji and all subfolders could not be recognized as modules, and Java files were outside of the source root as a consequence.

Solution:

  • set the system Java version (JDK) back to the previous set in problematic project (how to)
  • reopen IntelliJ idea workspace with this project
Drawbar answered 26/9, 2023 at 6:44 Comment(0)
L
0

Mark the directory as root solved my problem. Attaching screenshot. enter image description here

Labbe answered 18/3 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.