Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: package exists in another module: com.company.feature.apimodule in Intellij

I created a small project in Intellij Idea to mimic what I am seeing trying to migrate a large legacy codebase to OPENJDK16. Here is the directory structure:

$ tree 
/cygdrive/c/projects/play
 |--api
 |----api.iml
 |----src
 |------module-info.java (module com.company.feature.apimodule)
 |------com
 |--------company
 |----------feature
 |------------ApiRunnable.java
 |--home
 |----home.iml
 |----src
 |------module-info.java (module com.company.feature.homemodule)
 |------com
 |--------company
 |----------feature
 |------------Runnable1.java
 |--Modules
 |----.idea (Idea stuff in here)
 |----out
 |------production
 |--------api
 |--------home

module com.company.feature.apimodule
{
    exports com.company.feature;
}

package com.company.feature;
public interface ApiRunnable
{
    public void play(String[] strings);
}

module com.company.feature.homemodule
{
    requires com.company.feature.apimodule;       //Error Module not found
//    requires apimodule;                         //Error Module not found
//    requires com.company.feature.ApiRunnable;   //Error Module not found
}

package com.company.feature;
public class Runnable1 implements ApiRunnable
{
    @Override
    public void play(String[] strings)
    {
        int count = 1;
        for (String str : strings)
        {
            System.out.println("String " + count++ + " " + str);
        }
    }
    public static void main(String[] args)
    {
        Runnable1 myRun = new Runnable1();
        myRun.play(args);
    }
}

Note the directory structure is identical in api and home. This is intentional as my legacy codebase has this structure. This small example seems to duplicate the problems I am having and I can't figure out if the structure is the problem. It did compile and run fine using the unnamed-module.

Error #1 when doing a rebuild

C:\Projects\play\home\src\com\company\feature\Runnable1.java
java: package exists in another module: com.company.feature.apimodule

Does this mean that I can't have identical package trees in api and home?

Error #2

The homemodule cannot see the apimodule, I keep getting (Module not found) no matter what I put in the requires line.

Is this related to Error #1? If not, how do I fix this without resorting to the unnamed module for everything?

like image 664
AlexHomeBrew Avatar asked Oct 13 '25 07:10

AlexHomeBrew


1 Answers

@foo You are correct. The real solution is to not allow duplicate package names in different modules. This is not good coding practice but hey, I inherited it and this solution forced me to clean it up.

I solved this problem by renaming 5 packages to unique names and updating hundreds of import statements. Though tedious, it was relatively brainless work.

Since the modules identify things inside their src directory, they don't know which package is being referred to, the one in api.src or home.src.

I should have come back and updated this question, so thanks again @foo for reminding me.

like image 114
AlexHomeBrew Avatar answered Oct 14 '25 22:10

AlexHomeBrew