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.
asked Aug 21, 2020 at 10:34
AbhishekAbhishek
1,0071 gold badge11 silver badges15 bronze badges
6
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.
answered Sep 23, 2020 at 8:07
GeertPtGeertPt
16.4k3 gold badges37 silver badges61 bronze badges
1
Very simple solution: —
Click on «maven» on the right toolbar in IntelliJ and then click on the refresh button.
answered Oct 13, 2020 at 17:37
HokkyokuseiHokkyokusei
1,02113 silver badges18 bronze badges
4
I fixed it using this way:
File > Project Structure > Modules > Sources
and then marked java files dir as Sources.
Elletlar
3,1467 gold badges32 silver badges38 bronze badges
answered Nov 21, 2020 at 12:17
Sunny GuptaSunny Gupta
7198 silver badges7 bronze badges
0
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.
answered Sep 23, 2020 at 7:46
SlinnShadySlinnShady
4354 silver badges10 bronze badges
1
To Solve: «Java file outside of source root»
- Open Project Structure.
- Select Modules in Project Settings in left pane.
- Click on the folder in which your Java files (showing file outside of source root) are present.
- Then Select Sources from Mark as: option.
- Apply the same steps for multiple folders.
answered Jan 26, 2021 at 17:19
razorblackrazorblack
4014 silver badges6 bronze badges
4
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..
answered Apr 14, 2022 at 8:35
JCompetenceJCompetence
7,0473 gold badges19 silver badges26 bronze badges
2
Right click on pom.xm >> maven >> Reload project
answered Jul 4, 2021 at 17:02
2
Resolved !!
Go to your project on IntelliJ -> Right click on pom.xml -> Add as Maven Project.
answered May 26, 2022 at 14:39
See..See..
851 silver badge7 bronze badges
Right click on pom.xml then click on «maven» and reload project
answered Feb 2 at 12:40
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».
answered Apr 29, 2021 at 9:36
WesternGunWesternGun
11.3k6 gold badges88 silver badges157 bronze badges
1
In intellij:
click Maven,
select the project and right,
select Unignore Projects
answered Sep 23, 2021 at 20:16
2
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.
answered Oct 17, 2022 at 2:22
1
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:
answered May 30, 2022 at 14:19
Quan TruongQuan Truong
1972 silver badges11 bronze badges
I did a ‘Maven reload’ (or Gradle reload) from the upper-right ‘Maven’ (‘Gradle’) window and problem is solved.
answered Apr 29, 2022 at 13:36
ylevylev
2,3231 gold badge23 silver badges16 bronze badges
File
->
Project Structure
->
Module
select the Sources tab in the third pane from the left
answered May 2, 2022 at 15:16
0
Go to
File >>Project Structure>>Modules>> Import Module>> (At this point your src folder will appear on the right most pane automatically ) >> Apply >> OK
dippas
58.6k15 gold badges114 silver badges126 bronze badges
answered Feb 24, 2021 at 11:54
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.
answered Feb 16, 2022 at 18:37
chrischris
2,5104 gold badges32 silver badges56 bronze badges
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
.
answered Jun 13, 2022 at 16:47
algridalgrid
5,6003 gold badges34 silver badges37 bronze badges
Go to Intelij
File > Project Structure > Modules > Add
add module
answered Oct 17, 2022 at 2:50
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
answered May 25 at 10:50
DinithiDinithi
5185 silver badges17 bronze badges
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.
answered May 25 at 17:05
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.
After that, I did check the following check boxes.
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!
answered May 30 at 9:26
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’
answered Oct 28, 2021 at 10:30
marcz2007marcz2007
1671 gold badge1 silver badge11 bronze badges
I had to open the module I worked on to a new window to solve that.
answered Jan 18, 2022 at 15:50
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.
answered Jan 19, 2022 at 5:00
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.
answered Feb 18, 2022 at 12:22
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.
answered Nov 4, 2022 at 9:04
Ayush ShuklaAyush Shukla
4191 gold badge4 silver badges10 bronze badges
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.
answered Jan 4 at 17:52
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.
answered Jan 6 at 15:04
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
answered Jan 25 at 8:05
1
Java file outside of source root is an error message in IntelliJ IDEA when it does not consider the location of your Java project files as the real source. This is a difficult error that can lead to hours of debugging, but don’t worry, this article is your final stop for a solution. It’s because this article get insights from experts who have dealth with this error in IntelliJ.
By the end of this post, you’ll know why this error occurs and how you can fix it; so, read on.
Contents
- Why Is a Java Source File Outside the “Source” Root Error Happening?
- – You Used Intellij to Clone a Project With Multiple Branches
- – You Switched Your Project From Single Module to Multi Module
- – Your Project Files Are Not in the “Src/Main/Java” Folder
- – Intellij Does Not Recognize Your Maven Project
- How to Fix A Java File That Is Outside the “Source” Root
- 1. Add “POM.xml” as a Maven Project
- 2. Refresh Maven
- 3. Mark the “Java” Directory as a Source
- 4. Reselect the “Src” Using “Add Content Root”
- 5. Perform Source Generation and Folder Updates
- 6. Unlink and Re-add Maven Project
- 7. Use “POM.xml” To Identify Your Maven Project
- Conclusion
Why Is a Java Source File Outside the “Source” Root Error Happening?
You see Java source file is not in the “source” root error because:
- You used IntelliJ to clone a project with multiple branches
- Switching project from a single module to multi module
- Your project files are not in the “src/main/java” folder
- IntelliJ does not recognize your Maven project
– You Used Intellij to Clone a Project With Multiple Branches
When you clone a project with multiple branches from a remote repository, it can lead to an error in IntelliJ. That’s because IntelliJ does not import your project structure from the repository. As a result, it cannot know if you have your Java source code in the correct location. This applies if you’re using GitHub or GitLab with Maven or Gradle build systems.
– You Switched Your Project From Single Module to Multi Module
A switch from a single module to a multi module can lead to the error message that brought you here. Here is why: single module projects in Maven have a “pom.xml” file that IntelliJ can use to keep track of project dependencies. Meanwhile, multiple modules will have different modules with their “pom.xml” files. If at some point you change this single module to a multi module, you’ll confuse IntelliJ.
That’s because IntelliJ does not know how to keep track of the new “pom.xml” that you’ve added to your project. Besides, this “pom.xml” is the blueprint that IntelliJ will use to understand your project. If it does not exist, or it does not understand it, you’ll get an error.
– Your Project Files Are Not in the “Src/Main/Java” Folder
If you have a Maven project and your project files are not in the “src/main/java” folder, you’ll get an error. This happens because the “src/main/java” folder is the standard folder for a Maven project. As a result, IntelliJ will expect that your source codes must be in this folder before it can work with it. Still, if you don’t put your source code in this folder, an error will occur.
– Intellij Does Not Recognize Your Maven Project
In IntelliJ, if the output path is not specified for modules, you’ll get a compilation error. You’ll also get an error if IntelliJ does not recognize your Maven project. You’ll see this when IntelliJ changes the color of “pom.xml” and adds a line across it. From this, it’ll report that a Java source code is not in the “source” root.
Fix the error using the methods listed below:
- Add “pom.xml” as a Maven project
- Refresh Maven
- Mark the “java” directory as a source
- Reselect the “src” using “Add Content Root”
- Perform source generation and folder updates
- Unlink and re-add the Maven project
- Use “pom.xml” to identify your project
1. Add “POM.xml” as a Maven Project
A major fix for the IntelliJ java file outside of source root Maven error is to add the “pom.xml” file as a Maven project. You can do this using the following steps:
- Locate the “pom.xml” file.
- Right-click on it, and select “Add as a Maven project” from the context menu.
From this, IntelliJ will mark your project folders, and it will import any project dependencies.
2. Refresh Maven
You can solve the Spring boot Java file outside of source root error if you refresh Maven in IntelliJ. To do this, use the following steps:
- Locate the right toolbar.
- Click on “Maven” and select the “refresh” button.
Once you press the “refresh” button, IntelliJ will parse the project structure. Also, if your project has subprojects and dependencies, IntelliJ will reload everything. What’s more, in the latest version of IntelliJ, you can configure auto-reload using the following steps:
- Press the “Ctrl” + “Alt” + “S” at the same time on your keyboard.
- Navigate to “Build, Execution, Deployment”.
- Click on “Build tools”.
- Select “Any changes” if you want IntelliJ to reload everything when you make a change.
- Choose “External changes” if you want IntelliJ to reload if there is a change from your file system or version control system.
3. Mark the “Java” Directory as a Source
The “mark directory as test sources root IntelliJ” settings is a way to solve a Java source file that’s outside the “source” root. You can do this using the following steps:
- Select “File” from the top menu bar.
- Click “Project Structure” and choose “Modules”.
- Select “Sources” and mark the “java” directory as a source.
From this action, IntelliJ will know that the “java” directory contains the source for your project. Besides, by default, the IntelliJ Content Root is defined for modules and the “source” root is “src/main/java”. You configured the latter when you performed the previous steps. As a result, it will no longer show an error about a Java source file.
4. Reselect the “Src” Using “Add Content Root”
Another method to solve the “Java file outside of source root IntelliJ Gradle project” error is to add a content root. You can do this using the following steps:
- Launch IntelliJ and open your project.
- Click on “File” from the top menu bar.
- Select “Project Structure” from the context menu
- Choose “Modules” and remove all files from the “root”.
- Use “Add Content Root” and select the “src” folder”.
- Click “Apply” and save your changes.
Once you click “Apply”, IntelliJ will see your Java source files as they are in your project structure.
5. Perform Source Generation and Folder Updates
IntelliJ can find it difficult to recognize your generated source code, as a result, you can not work with them within the IDE. But, regenerate Maven sources and update folders is a way to fix this issue. Now, open your IntelliJ, and do the following:
- Locate the “pom.xml” file.
- Right-click on it and click “Maven”.
- Select “Generate Sources and folder updates” from the context menu.
6. Unlink and Re-add Maven Project
This unlinking method is a way to solve the “source” root error when you change a single-module project to a multi-module project. To do this, open IntelliJ and do the following:
- Select the “pom.xml” in the root folder of your project.
- Click “Maven Unlink”.
- This will remove all submodules.
- Select the root “pom.xml”.
- Click on “Add as Maven project” from the context menu.
When you complete the previous steps, everything will work as expected.
7. Use “POM.xml” To Identify Your Maven Project
When IntelliJ does not recognize your Maven project, you can use the “pom.xml” file to fix it. The following steps show you how to do this:
- Right-click on “pom.xml”.
- Select “Maven – Unignore projects”.
The last command will let IntelliJ recognize your Maven project, and it will import your project dependencies.
Conclusion
This article detailed the causes and fixes of a Java source file that’s outside the “source” root in IntelliJ. The following is the summary of our discussion:
- When you clone a remote repository in IntelliJ, it can lead to a Java source file that’s outside the “source” root”.
- If you switch a single module to a multi module, you’ll cause the “source” root error in IntelliJ.
- A fix for the source root IntelliJ error is the selection of the “java” folder as a source of your project.
- You can use the “pom.xml” file to identify your Maven projects.
- When you change “pom.xml”, refresh Maven to prevent errors about the source of your project.
With everything that you’ve learned in this article, you can work with Java in IntelliJ without errors. If you have a colleague that’s facing this error, you should show them our article.
- Author
- Recent Posts
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team
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.
asked Aug 21, 2020 at 10:34
AbhishekAbhishek
1,0071 gold badge11 silver badges15 bronze badges
6
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.
answered Sep 23, 2020 at 8:07
GeertPtGeertPt
16.4k3 gold badges37 silver badges61 bronze badges
1
Very simple solution: —
Click on «maven» on the right toolbar in IntelliJ and then click on the refresh button.
answered Oct 13, 2020 at 17:37
HokkyokuseiHokkyokusei
1,02113 silver badges18 bronze badges
4
I fixed it using this way:
File > Project Structure > Modules > Sources
and then marked java files dir as Sources.
Elletlar
3,1467 gold badges32 silver badges38 bronze badges
answered Nov 21, 2020 at 12:17
Sunny GuptaSunny Gupta
7198 silver badges7 bronze badges
0
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.
answered Sep 23, 2020 at 7:46
SlinnShadySlinnShady
4354 silver badges10 bronze badges
1
To Solve: «Java file outside of source root»
- Open Project Structure.
- Select Modules in Project Settings in left pane.
- Click on the folder in which your Java files (showing file outside of source root) are present.
- Then Select Sources from Mark as: option.
- Apply the same steps for multiple folders.
answered Jan 26, 2021 at 17:19
razorblackrazorblack
4014 silver badges6 bronze badges
4
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..
answered Apr 14, 2022 at 8:35
JCompetenceJCompetence
7,0473 gold badges19 silver badges26 bronze badges
2
Right click on pom.xm >> maven >> Reload project
answered Jul 4, 2021 at 17:02
2
Resolved !!
Go to your project on IntelliJ -> Right click on pom.xml -> Add as Maven Project.
answered May 26, 2022 at 14:39
See..See..
851 silver badge7 bronze badges
Right click on pom.xml then click on «maven» and reload project
answered Feb 2 at 12:40
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».
answered Apr 29, 2021 at 9:36
WesternGunWesternGun
11.3k6 gold badges88 silver badges157 bronze badges
1
In intellij:
click Maven,
select the project and right,
select Unignore Projects
answered Sep 23, 2021 at 20:16
2
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.
answered Oct 17, 2022 at 2:22
1
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:
answered May 30, 2022 at 14:19
Quan TruongQuan Truong
1972 silver badges11 bronze badges
I did a ‘Maven reload’ (or Gradle reload) from the upper-right ‘Maven’ (‘Gradle’) window and problem is solved.
answered Apr 29, 2022 at 13:36
ylevylev
2,3231 gold badge23 silver badges16 bronze badges
File
->
Project Structure
->
Module
select the Sources tab in the third pane from the left
answered May 2, 2022 at 15:16
0
Go to
File >>Project Structure>>Modules>> Import Module>> (At this point your src folder will appear on the right most pane automatically ) >> Apply >> OK
dippas
58.6k15 gold badges114 silver badges126 bronze badges
answered Feb 24, 2021 at 11:54
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.
answered Feb 16, 2022 at 18:37
chrischris
2,5104 gold badges32 silver badges56 bronze badges
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
.
answered Jun 13, 2022 at 16:47
algridalgrid
5,6003 gold badges34 silver badges37 bronze badges
Go to Intelij
File > Project Structure > Modules > Add
add module
answered Oct 17, 2022 at 2:50
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
answered May 25 at 10:50
DinithiDinithi
5185 silver badges17 bronze badges
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.
answered May 25 at 17:05
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.
After that, I did check the following check boxes.
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!
answered May 30 at 9:26
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’
answered Oct 28, 2021 at 10:30
marcz2007marcz2007
1671 gold badge1 silver badge11 bronze badges
I had to open the module I worked on to a new window to solve that.
answered Jan 18, 2022 at 15:50
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.
answered Jan 19, 2022 at 5:00
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.
answered Feb 18, 2022 at 12:22
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.
answered Nov 4, 2022 at 9:04
Ayush ShuklaAyush Shukla
4191 gold badge4 silver badges10 bronze badges
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.
answered Jan 4 at 17:52
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.
answered Jan 6 at 15:04
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
answered Jan 25 at 8:05
1
“Java file outside of source root” is a common error message in IntelliJ IDEA. It simply said that you need to add the file to the project root before doing anything.
For those who are beginning with Java, the error might become a little trouble. In this short article, we will list a few quick fixes for “Java file outside of source root”.
Import the module from pom.xml for Maven projects
If you’re working on a Maven project, you would have to import import the module from pom.xml so that it configures the roots automatically.
Sources root is marked in blue in the project view. Here is the example of the properly configured project.
Maven roots will be configured following its Standard Directory Layout, which Maven expects from a regular project.
In order to add a Maven module to the project in which you are already working.
- In the Project tool window, right-click the project folder and select New > Module. Alternatively, from the main menu, select File > New > Module to open the New Module wizard.
- If you used main menu to add a module then the process of adding a module is the same as Creating a new Maven project. If you are adding sub modules by right-clicking the root folder then the process of adding a new module is shorter. You need to specify the name of your module in the Name field. The rest of the information is added automatically and you can use either the default settings or change them according to your preferences.
Manually mark a folder as source root
Alternatively, you can manually set a folder to be the source root from the sidebar of IntelliJ.
Simply right click on the src folder and select “Mark Directory as” > “Sources Root”.
You can also do this by configuring project modules:
- Open file->Project Structure , select Modules.
- Select the src folder and choose Sources in the Mark as section, then click Apply, which solves the problem of Source Root.
Regenerate Maven Sources and Update Folders
If you are using Maven and Intellij Idea does not include generated sources folder automatically, the following option works in majority of the cases.
- Right click on your project
- Choose Maven > Generate sources and update folders
Alternatively, you can use the Maven sidebar and click on the folder with spinner icon to activate Generate Sources and Update Folders for All Projects.
If none of the above solutions worked, here’s a few more suggestions:
- Move the Java files into the source code root directory. This may require updating your project configuration or build tools settings accordingly.
- Add the Java file to your project source file path. This can be done in the configuration file of the project build tool (such as Maven or Gradle) or in the project settings in the IDE. Make sure you have all required libraries and dependencies in your classpath.
- Change the Java compiler options to allow the compiler to look for Java files in non-standard source root directories. For example, when using the javac command, you can use the -sourcepath option to specify the location of your source files.
- If you must keep your Java files outside of your Java files, make sure they are in the correct directory structure with the correct package names and import statements. Also, make sure your compiler or IDE is configured with the correct classpath and environment with all required libraries and dependencies.
Having read to the end of this article, it is clear that you’re getting started with Java. In that case, No string-argument constructor/factory method to deserialize from string value is another common error in Spring Framework which you might find interesting to find out more about.
In this guide, we will walk you through the process of fixing the «Java file outside of source root» issue in IntelliJ IDEA. This issue can be quite frustrating, as it prevents you from running your Java programs and may lead to other complications in your development environment. Following the steps outlined in this guide will help you resolve this issue and get back to writing and running your Java programs seamlessly.
Table of Contents
- Understanding the Issue
- Step-by-Step Solution
- FAQ
Understanding the Issue
Before diving into the solution, it’s essential to understand the underlying issue. IntelliJ IDEA is a popular integrated development environment (IDE) for Java developers. When you create a Java project in IntelliJ, it automatically sets up a proper directory structure, including a src
(source) folder where your Java code should reside.
However, you may encounter the «Java file outside of source root» issue if your Java file is not located within the designated src
folder or if IntelliJ is not correctly recognizing the folder as the source root.
Source: JetBrains
Step-by-Step Solution
To fix the «Java file outside of source root» issue in IntelliJ, follow these steps:
Locate the problematic Java file: Figure out which Java file is causing the issue. This is usually indicated by a yellow warning icon next to the file in the Project tool window.
Move the Java file to the src folder: If the Java file is not located within the src
folder, move it there. To do this, simply click and drag the file to the appropriate location in the Project tool window.
Mark the src folder as the source root: If the Java file is already within the src
folder but IntelliJ is not recognizing it as the source root, you need to mark the folder as the source root manually. To do this, follow these steps:
- Right-click the
src
folder in the Project tool window. - Select «Mark Directory As» from the context menu.
- Click «Sources Root» in the submenu.
After completing these steps, IntelliJ should now recognize the Java file as part of the source root, and the issue should be resolved.
FAQ
How do I create a new src folder in IntelliJ?
If your project does not have a src
folder, you can create one by following these steps:
- Right-click your project’s root folder in the Project tool window.
- Select «New» from the context menu.
- Click «Directory.»
- Name the new directory «src» and press Enter.
Don’t forget to mark the new src
folder as the source root.
Why is it important to have a src folder in a Java project?
Having a dedicated src
folder in your Java project is essential because it helps maintain a clean and organized project structure. The src
folder is where all your Java source files should be placed, making it easy to locate and manage your code.
Can I have multiple source roots in a single IntelliJ project?
Yes, you can have multiple source roots in a single IntelliJ project. To mark additional folders as source roots, simply follow the same steps as marking the src
folder as the source root for each additional folder.
What other types of folders can I mark in IntelliJ?
In addition to marking folders as source roots, IntelliJ allows you to mark folders as:
- Test Sources Root: Contains the source code for your unit tests.
- Resources Root: Contains non-code resources, such as images, audio files, or configuration files.
- Test Resources Root: Contains non-code resources specifically for your unit tests.
- Excluded: Marks a folder to be excluded from the project, meaning IntelliJ will not index or compile its contents.
Why are my Java files still not recognized after marking the src folder as the source root?
If your Java files are still not recognized after marking the src
folder as the source root, try the following:
- Invalidate caches and restart IntelliJ: Go to
File
>Invalidate Caches / Restart...
>Invalidate and Restart
. - Re-import the project: Close the project, delete the
.idea
folder, and re-import the project into IntelliJ. - Check for updates: Ensure that you are using the latest version of IntelliJ IDEA by going to
Help
>Check for Updates...
.
If the issue persists, consider reaching out to JetBrains support for further assistance.
- Configuring Content Roots in IntelliJ IDEA
- IntelliJ IDEA Project Structure
- Working with Source Roots in IntelliJ IDEA
If you have any further questions or need additional assistance, feel free to ask in the comments below. Happy coding!