Java file outside of source root intellij idea что делать
Перейти к содержимому

Java file outside of source root intellij idea что делать

  • автор:

Файл Java за пределами исходного корня intelliJ

Я клонировал проект весенней загрузки из GitLab, имеющий несколько веток в IntelliJ. Я оформляю заказ в филиал, в котором хочу работать. Но я получаю «файл Java за пределами исходного корня» для всех файлов. Пожалуйста, подскажите, что я делаю не так. Я только что установил IntelliJ, и это первый проект, который я импортирую.

27 ответов

Если вы выполняете «импорт из git», IntelliJ не импортирует структуру проекта из maven (или gradle) автоматически.

Один из способов сделать это впоследствии — щелкнуть правой кнопкой мыши файл pom.xml в корневом каталоге и выбрать «Добавить как проект maven». Тогда папки будут правильно помечены, и зависимые библиотеки будут импортированы. Не должно быть необходимости вручную отмечать отдельные исходные папки.

Я предпочитаю делать клон git вне IntelliJ и открывать проект из локальной файловой системы, а затем IntelliJ автоматически импортирует структуру проекта maven.

Очень простое решение:-

Нажмите «maven» на правой панели инструментов в IntelliJ, а затем нажмите кнопку обновления.

[Solved]-Java file outside of source root intelliJ-Springboot

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.

GeertPt 14568

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'

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

Anwar Benhamada 191

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.

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

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

chris 1839

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.

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

Select root pom.xml —> Add As Maven project

Then everythings worked..

JCompetence 4429

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

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

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

Regenerate Maven Sources and Update Folders

When moving the project from one directory to another, Maven will need to regenerate the sources to tell the project to use Annotation Processing.

Try this

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 for me, in May 2022, as the answers have seemed to vary over time:

enter image description here

Quan Truong 124

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 .

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

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".

WesternGun 9340

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

Diego Virgüez 135

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

razorblack 121

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.

SlinnShady 325

I fixed it using this way:

and then marked java files dir as Sources.

Very simple solution :-

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

Fixing the Java File Outside of Source Root Issue in IntelliJ: A Comprehensive Guide

Gary Vanpelt

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

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.

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:

  1. Right-click the src folder in the Project tool window.
  2. Select «Mark Directory As» from the context menu.
  3. 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.

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:

  1. Right-click your project’s root folder in the Project tool window.
  2. Select «New» from the context menu.
  3. Click «Directory.»
  4. Name the new directory «src» and press Enter.

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:

  1. Invalidate caches and restart IntelliJ: Go to File > Invalidate Caches / Restart. > Invalidate and Restart .
  2. Re-import the project: Close the project, delete the .idea folder, and re-import the project into IntelliJ.
  3. 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.

Related Links

If you have any further questions or need additional assistance, feel free to ask in the comments below. Happy coding!

Java file outside of source root intelliJ

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.

33 Answers 33

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.

GeertPt's user avatar

Very simple solution: —

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

Hokkyokusei's user avatar

I fixed it using this way:

and then marked java files dir as Sources.

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.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *