Unable to find modules from module-info.java
Asked Answered
C

2

7

I have 2 different projects in the same folder: com.jdojo.address and com.jdojo.person. The first has a simple pojo class Address and a module-info.java under default package:

module com.jdojo.address {
    exports com.jdojo.address;
}

No errors here.

In the com.jdojo.person project I have a Person class that uses an Address attribute from the previous one. The module-info.java:

module com.jdojo.person {
    requires com.jdojo.address;
    exports com.jdojo.person;
}

I have an error in the requiresline:

module not found: com.jdojo.address

I have this issue both with NetBeans IDE Dev (Build 201708030001) and IntelliJ IDEA 2017.2.1.

├───com.jdojo.address
│   ├───.idea
│   │   └───inspectionProfiles
│   ├───nbproject
│   │   └───private
│   └───src
│       └───com
│           └───jdojo
│               └───address
├───com.jdojo.intro
│   ├───.idea
│   │   ├───inspectionProfiles
│   │   └───libraries
│   ├───build
│   │   ├───classes
│   │   │   └───com
│   │   │       └───jdojo
│   │   │           ├───address
│   │   │           ├───intro
│   │   │           └───person
│   │   ├───empty
│   │   └───generated-sources
│   │       └───ap-source-output
│   ├───dist
│   ├───nbproject
│   │   └───private
│   ├───src
│   │   └───com
│   │       └───jdojo
│   │           └───intro
│   └───test
├───com.jdojo.person
│   ├───.idea
│   │   └───inspectionProfiles
│   ├───nbproject
│   │   └───private
│   ├───src
│   │   └───com
│   │       └───jdojo
│   │           └───person
│   └───test
├───lib
├───mods
│   └───com.jdojo.intro
│       └───com
│           └───jdojo
│               └───intro
└───src
    └───com.jdojo.intro
        └───com
            └───jdojo
                └───intro

These are the module-info and java classes paths

C:\Java9Revealed\com.jdojo.address\src\module-info.java
C:\Java9Revealed\com.jdojo.address\src\com\jdojo\address\Address.java

C:\Java9Revealed\com.jdojo.person\src\module-info.java
C:\Java9Revealed\com.jdojo.person\src\com\jdojo\person\Person.java
Cider answered 4/8, 2017 at 13:51 Comment(2)
Is it the exact same message in IntelliJ? When just adding a random module as dependency, it appears red in the module info because the IntelliJ modules also need to have the corresponding dependency. Otherwise you get "Module is not in dependencies: ..." but a quick fix will add it. If that's not the problem, can you share the exact folder layout?Molloy
In Intellij i have the same message 'module not found: com.jdojo.address'. I've added a tree of the folder if that can help, i've left the other previous project 'com.jdojo.intro'.Cider
U
7

Per the Quick Start guide, it is convention to put the source-code in a folder named after the module. In this case, the folder structure should be:

com.jdojo.address/src/com.jdojo.address/module-info.java
com.jdojo.address/src/com.jdojo.address/com/jdojo/address/Address.java

com.jdojo.person/src/com.jdojo.person/module-info.java
com.jdojo.person/src/com.jdojo.person/com/jdojo/person/Person.java

It is illuminating to experiment using command-line tools, independent of any IDE. I have illustrated this case here.

Unpeg answered 4/8, 2017 at 18:27 Comment(6)
I've added them at the endCider
Sadly, I don't know what this means. What did you add?Unpeg
At the end of the post i've added my Address, Person and module-info pathsCider
@MichaelEaster Any links to why such naming convention(directory structrue) was chosen in Java9?Boatwright
@nullpointer The first blog post for Project Jigsaw was in December 2008. There are mailing list archives but it would be quite a task to find the rationale for the design. (It is quite reasonable in practice.)Unpeg
This Quick Start convention goes against the standard Java project directory structure, like the one followed by Eclipse IDE. Besides, I don't see how using the module name as parent directory of the package fo the source file is relevant to the run-time jar. This parent directoy will not show up anywhere in the run-time jar.Cureton
E
0

Placement of the module-info.java

For me, the key was the placement of the module-info.java as well as the naming.

I am migrating a multi module maven project from Java 11 to Java 17. In Java 11, the project builds correctly until you add the module-info.java files. Then you may find different kinds of errors, ranging from "X module not visible" to "JavaDoc: no sources for module Y."

I generated automatically the module-info.java files with jdeps. This meant that the java modules were named after the maven project modules, which did not coincide with any java package name.

Renaming the module declaration to the actual java package, was a first step.

Then I tried to follow the placement of the module-info.java file according to the answers here but at the end, having one java module per maven module, was good enough to place the module-info.java inside the src/main/java directory, right where the com/company/application started for a module like com.company.application.api (instead of where the sources were, as I was told several places).

So, not src/main/java/com/company/application/api/module-info.java but src/main/java/module-info.java), for a module-info.java like this:

module com.company.application.api {
    exports com.company.application.api;
}
Edouard answered 14/11, 2023 at 18:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.