Java file outside of source root intelliJ – Java

Photo of author
Written By M Ibrahim
intellij-idea java spring-boot

The Problem:

I cloned a spring boot project from GitLab, but when I try to import it into IntelliJ, I get the error "Java file outside of source root" on all the files. How can I resolve this issue to successfully import the project?

The Solutions:

Solution 1: Manually Add as Maven Project

When importing a project from Git using IntelliJ, it may not automatically detect the project structure from Maven (or Gradle). To resolve this:

  1. Right-click on the pom.xml file in the root directory.
  2. Select "Add as Maven Project".

Afterward, the folders will be marked correctly, and dependent libraries will be imported, removing the "Java file outside of source root" error.

Solution 3: Including Java Files in Source Root

To resolve the “Java file outside of source root” error in IntelliJ, follow these steps:

  1. Open the "Project Structure" dialog:
    • Go to File > Project Structure.
  2. Select the Modules tab in the left pane.
  3. In the "Sources" tab, locate the directory where your Java files are located.
  4. Ensure that the checkbox next to the directory is marked as Sources.
  5. Click OK to save the changes.

This will add the Java files to the source root, allowing IntelliJ to recognize them and resolve the error.

Solution 4: Fix Project Structure

To resolve the "Java file outside of source root" issue:

  • Go to File > Project Structure.
  • Under Modules, locate the root of the project and remove all the files listed by clicking the "x" marks on the far right.
  • Click the "+ Add Content Root" button.
  • Select the src folder containing your .java files and click Apply.
  • This will add the src folder as a content root to the project, and IntelliJ will now recognize the .java files within it.

Solution 5: To Solve: *”Java file outside of source root”*

  1. Open Project Structure (File -> Project Structure…).
  2. Expand Modules in Project Settings (on the left pane).
  3. Select the folder where your Java files (displaying "Java file outside of source root") are located.
  4. Choose Sources from the Mark as option.
  5. Repeat steps 3-4 for any additional folders as needed.

This will mark your Java files as sources and should resolve the "Java file outside of source root" issue.

Image showing the mark as Source option