IntelliJ
Java
Source Root
Project Structure
File Management

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:

text
1project-root/
2  src/
3    main/
4      java/          <-- Sources Root (blue folder)
5      resources/     <-- Resources Root
6    test/
7      java/          <-- Test Sources Root (green folder)
8      resources/     <-- Test Resources Root
9  build/
10    generated/
11      sources/       <-- Generated Sources Root (if applicable)

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:

  1. Open the Project tool window (Alt+1 / Cmd+1)
  2. Right-click the directory that should be a source root
  3. 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:

  1. Open File then Project Structure (Ctrl+Alt+Shift+S / Cmd+;)
  2. Select Modules in the left panel
  3. Click the module containing the file
  4. Open the Sources tab
  5. Click the folder you want to mark in the directory tree
  6. Click the Sources button above the tree (or right-click and select the root type)
  7. Click Apply then OK
text
1Project Structure > Modules > [your module] > Sources tab
2
3Directory tree:
4  [project-root]
5    [src]
6      [main]
7        [java]  <-- click this, then click "Sources" button
8      [test]
9        [java]  <-- click this, then click "Tests" button

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:

text
1For Maven:
2  Right-click pom.xml > Maven > Reimport
3  Or: Maven tool window > Reload button
4
5For Gradle:
6  Right-click build.gradle > Gradle > Reimport Project
7  Or: Gradle tool window > Reload button
8  Or: File > Invalidate Caches and Restart (nuclear option)

After re-import, IntelliJ sets the source roots from the sourcesets (Gradle) or the standard directory layout (Maven).

Causes and fixes

CauseSymptomFix
Project not imported as Maven/GradleNo blue/green folders, all files show warningRight-click pom.xml or build.gradle and select "Import"
Non-standard directory layoutFiles in src/ instead of src/main/java/Either restructure to standard layout or mark manually
Module not configuredFile is in a directory IntelliJ does not associate with any moduleAdd the directory as a module or content root in Project Structure
.idea directory corruptedSource roots lost after IDE update or branch switchDelete .idea/ directory and re-import the project
Multi-module project misconfiguredSome modules show the warning, others do notCheck each module's source roots in Project Structure
Git checkout created new directoriesNew directories from another branch are not auto-detectedRe-import or manually mark new directories

Checking the fix and the package path

After marking the roots, confirm IntelliJ recognizes the file:

java
1// This file should now show:
2// - Correct package declaration (matching directory structure)
3// - Working code completion (Ctrl+Space)
4// - No yellow background
5// - Ability to run/debug
6package com.example.myapp;
7
8public class Application {
9    public static void main(String[] args) {
10        System.out.println("File is inside source root");
11    }
12}

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.

text
1Correct:
2  Source root: src/main/java/
3  File path:   src/main/java/com/example/Main.java
4  Package:     com.example
5
6Wrong (source root too deep):
7  Source root: src/main/java/com/
8  File path:   src/main/java/com/example/Main.java
9  Package:     example  (IntelliJ expects this, but file declares com.example)
10
11Wrong (source root too shallow):
12  Source root: src/
13  File path:   src/main/java/com/example/Main.java
14  Package:     main.java.com.example  (IntelliJ expects this based on directory depth)

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

groovy
1// build.gradle
2sourceSets {
3    main {
4        java {
5            srcDirs = ['src/main/java', 'src/generated/java']
6        }
7        resources {
8            srcDirs = ['src/main/resources', 'config']
9        }
10    }
11    test {
12        java {
13            srcDirs = ['src/test/java', 'src/integration-test/java']
14        }
15    }
16}

Maven

xml
1<!-- pom.xml -->
2<build>
3    <sourceDirectory>src/main/java</sourceDirectory>
4    <testSourceDirectory>src/test/java</testSourceDirectory>
5    <plugins>
6        <plugin>
7            <groupId>org.codehaus.mojo</groupId>
8            <artifactId>build-helper-maven-plugin</artifactId>
9            <executions>
10                <execution>
11                    <id>add-source</id>
12                    <phase>generate-sources</phase>
13                    <goals><goal>add-source</goal></goals>
14                    <configuration>
15                        <sources>
16                            <source>src/generated/java</source>
17                        </sources>
18                    </configuration>
19                </execution>
20            </executions>
21        </plugin>
22    </plugins>
23</build>

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 of src/main/java/ makes IntelliJ expect packages starting with main.java.com.example. The source root must be the directory directly above your top-level package.
  • Not re-importing after editing build.gradle or pom.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 .java files 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.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms