12

I'm working on a project in IntelliJ Idea using Kotlin. I'm trying to create a unit test for a Kotlin class, and I can create the class, except that when I run the test, I get an error that there is no JDK configured. I go into the project structure and change the JDK for the module, but when I click on Apply, I get a message saying that :

Content root "C:\Users\2rtmar\Documents\xproject\xproject\examples\src\main" is defined for modules "xproject-examples-utils_main" and "xproject-examples-java_main". Two modules in a project cannot share the same content root.

Another team member had said that these modules were faulty, and that they couldn't be used, but I'm not using them and still prevent me from running my code. I tried to unmark this module as a sources root, but did not fix the problem. I went as far as to removing the xproject\examples module entirely, but received the same message when trying to set the JDK of my module.

Any help is appreciated!

2
  • Same folder can not be used as a content root for two different modules, see details. If the modules are not used, try to mark them as ignored in Gradle tool window. Also try refreshing project from Gradle tool window and see if this helps. Otherwise corresponding build.gradle configuration file should be changed to fix this issue.
    – Andrey
    May 18, 2018 at 8:15
  • Getting into this issue while creating a multi-module Gradle project. My root project is a Gradle project, then I'v added a submodule which is not in the Gradle scope of the root module. When I go to the module settings and try to remove the submodule which is outside of the root AND inside of the root, I get this error and cannot save. It seems it cannot be solved without editing .XML files, which is very unfortunate. I think setting up a root module with submodules is and should be pretty much common without major issues out of the box Oct 23, 2021 at 11:44

3 Answers 3

5

In my case I was trying to setup a multi-module Gradle project by adding a new module to my root Gradle project.

Despite adding an include ':my-submodule' to my settings.gradle, it was always telling me gradle wrapper not found, because the submodule was kind of outside of the context of the root module.

This however is IntelliJ-specific

When I looked into Right-Click on module -> Open Module Settings - > Modules I was seeing the following hierarchy:

my-root
   -> my-submodule
my-submodule

For whatever reason, IntelliJ has added the new module as a child and as a standalone module, and this caused the issue Two modules cannot have the same content root, as my-root:my-submodule and my-submodule are now both pointing to the same src/main directory.

This is IntelliJ's out of the box behaviour when adding a new submodule, which is in my opinion totally wrong and likely a bug! You cannot even fix it within the module settings

Solution

But I discovered a way to fix it.

  1. Right-click on the module
  2. Load/Unload Modules
  3. Now unload the module that is not below the my-root

After that, there is just one single root pointing at the src directory and the active module is now within the parent's Gradle scope.

I hope Jetbrains fixes these issues. Those problems exist for a while now and are not pleasant to solve.

1
  • I tried this approach and it helped me remove the module but it was somehow still cached even when deleting .idea folder and it still had wrong paths when I recreated the module(was now in root project but the main kotlin was in old path again) and I couldn't change paths. Ultimately only the complete deletion of project and a new reimport from version control fixed this. There currently seems to be some bugs with the module creator wizards (create module over project structure or by ->New -> Module...) avoid it whenever possible(try to wire up your module manually by creating paths/folders).
    – S. H.
    Feb 24, 2023 at 15:05
2

I got this issue recently. The very first check should be to check if any other module with the same content root is added or not.[you can check this in Project Structure -> Modules]

If YES, remove the other project by clicking on - (minus) button at the top.

1

If you're using Intellij and have modified / are modifying the pom.xml files directly:

  1. Go to the affected sources' pom.xml files
  2. Go to the following area:
...
<build>
       ...
        <resources>
            <resource>
                    ...
                <directory>[DIRECTORY]</directory>
                    ...
                <includes>
                    ...
                </includes>
                    ...
            </resource>
        </resources>
       ...
    </build>
...

Change the [DIRECTORY] to something unique. For instance, I was referencing ../plugin-outputs. As such, I changed it to ../plugin-outputs/[specific sub-directory], wherein I named the sub directory as I wished.

  1. Delete any "modules" listing the specific output directory. No, they aren't modules... however, they have the module icon applied to this, preventing the pom.xml files' changes to be updated while editing modules through the standard project structure tab.

I know this thread is 2 years old; however, there are MANY people who still read these artifacts... hopefully this helps someone.

-Dan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.