IntelliJ: Java file is located outside of the module source root
Codemia Knowledge Hub ·
IntelliJ IDEA shows "Java file outside of source root" (with a yellow file background) when the file is not inside a directory marked as a source root, so it won't be compiled. Fix it by marking the right directory as a Sources Root, or by re-importing the Maven or Gradle project so IntelliJ sets the roots from the build file. This is an IDE warning, not a Java compiler error: IntelliJ cannot offer code completion, refactoring, or compilation for the file because it does not know where it belongs in the source tree.
What source roots are for
IntelliJ uses source roots to know which directories hold compilable source code, test code, resources, and generated files. It relies on them for:
- Resolving package declarations and imports
- Code completion and navigation
- Compiling with the correct classpath
- Running tests from the correct source sets
- Indexing only relevant files for search
A typical Maven or Gradle project has these standard roots:
A Java file outside all of these, or a project IntelliJ did not import correctly, triggers the warning.
Method 1: Mark Directory as Sources Root
The quickest fix for one directory:
- Open the Project tool window (Alt+1 / Cmd+1)
- Right-click the directory that should be a source root
- Select Mark Directory as then Sources Root
The folder icon turns blue and IntelliJ re-indexes its files.
Method 2: Project Structure dialog
For a full view of every module and its roots:
- Open File then Project Structure (Ctrl+Alt+Shift+S / Cmd+;)
- Select Modules in the left panel
- Click the module containing the file
- Open the Sources tab
- Click the folder you want to mark in the directory tree
- Click the Sources button above the tree (or right-click and select the root type)
- Click Apply then OK
Method 3: Re-import the Maven or Gradle project
For Maven and Gradle projects, the simplest fix is to let the build tool set the roots:
After re-import, IntelliJ sets the source roots from the sourcesets (Gradle) or the standard directory layout (Maven).
Causes and fixes
| Cause | Symptom | Fix |
| Project not imported as Maven/Gradle | No blue/green folders, all files show warning | Right-click pom.xml or build.gradle and select "Import" |
| Non-standard directory layout | Files in src/ instead of src/main/java/ | Either restructure to standard layout or mark manually |
| Module not configured | File is in a directory IntelliJ does not associate with any module | Add the directory as a module or content root in Project Structure |
.idea directory corrupted | Source roots lost after IDE update or branch switch | Delete .idea/ directory and re-import the project |
| Multi-module project misconfigured | Some modules show the warning, others do not | Check each module's source roots in Project Structure |
| Git checkout created new directories | New directories from another branch are not auto-detected | Re-import or manually mark new directories |
Checking the fix and the package path
After marking the roots, confirm IntelliJ recognizes the file:
If the package declaration does not match the directory path relative to the source root, you get a different error: "Package name does not correspond to the file path." The source root is set, but at the wrong level.
Non-standard source directories in Gradle and Maven
For non-standard layouts, declare the directories in the build file so IntelliJ picks them up on import.
Gradle
Maven
Re-import the project after editing the build file and IntelliJ will set the roots to match.
Mistakes that bring the warning back
- Marking the wrong level. Marking
src/instead ofsrc/main/java/makes IntelliJ expect packages starting withmain.java.com.example. The source root must be the directory directly above your top-level package. - Not re-importing after editing
build.gradleorpom.xml. IntelliJ does not pick up source set changes automatically. Trigger a re-import. - Deleting
.idea/without re-importing. Opening the directory without importing loses all module and source root configuration. - Creating Java files before setting up the project. If you turn a plain directory of
.javafiles into a project later, IntelliJ may not detect the structure. Import through the build tool first, then create files. - Confusing content roots with source roots. A content root is the top-level directory associated with a module. A source root is a subdirectory inside it that holds compilable code. Both must be configured correctly.
- Ignoring the warning because the command line build works. Maven and Gradle compile from their own configuration, independent of IntelliJ. The warning means IntelliJ features (refactoring, navigation, debugging) will not work correctly for that file.
Related reading
- Java FileOutputStream Create File if not exists
- Java, find intersection of two arrays
- Java function for arrays like PHP's join()?
- Java Future vs c async await
- Java FutureTask Exception
- Java G1 Old generation garbage collection count is 0
- Java Generate Random Number Between Two Given Values
- Java generics T vs Object

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.