Quick revision
The short answer
Start by mapping package dependencies with jdeps, then remove split packages and cycles, because the module system forbids both. Move bottom-up: turn leaf libraries into modules first while the rest of the application stays on the class path, and use automatic modules for third-party jars that have no module-info. Stop as soon as the encapsulation goals are met, because for many applications ArchUnit rules give similar boundaries at much lower cost.
What to remember
In this answer, we saw that modularising a monolith starts with a jdeps map, then removing split packages and cycles, which the module system forbids and which hurt the codebase anyway. We migrate bottom-up because named modules cannot read the class path, use automatic modules for third-party jars, and open packages to Spring and Hibernate for reflection. We stop when the boundaries we wanted are enforced, and we keep in mind that ArchUnit or Spring Modulith can deliver similar enforcement for an application at much lower cost.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Start by mapping package dependencies with jdeps, then remove split packages and cycles, because the module system forbids both. Move bottom-up: turn leaf libraries into modules first while the rest of the application stays on the class path, and use automatic modules for third-party jars that have no module-info. Stop as soon as the encapsulation goals are met, because for many applications ArchUnit rules give similar boundaries at much lower cost.
Why this question is asked
Large monoliths drift into a tangle where everything can call everything, and the module system promises enforced boundaries. Interviewers ask this question to see whether we know the real obstacles, such as split packages, cycles and reflection-heavy frameworks, and whether we can judge when the effort stops paying for itself. In this answer, we will recap what a module requires, map the code with jdeps, clean up the blockers, migrate bottom-up, deal with Spring and Hibernate, and decide where to stop.
1A Quick Recap of What a Module Requires
A module is a jar with a module-info.java file that gives it a name, lists the modules it requires and lists the packages it exports. Only exported packages can be used by other modules, so internal packages are truly hidden, even if their classes are public. That enforcement is the value we are after.
The module system adds three rules that most monoliths break. A package may live in only one module; two jars sharing a package, called a split package, cannot both be modules. Module dependencies may not form a cycle: if billing requires customer, customer cannot require billing. And a named module cannot read code on the class path, so a module cannot depend on code that has not been modularised yet. The third rule is why the order of migration matters.
2Mapping the Code With jdeps
Before changing anything, we need a picture of the dependencies between our own packages and jars. jdeps produces it.
Notice that the summary already shows the most important problem: billing and customer depend on each other. The DOT output lets us find which packages cause it and lists packages that appear in more than one jar. The generated module-info is only a draft; it exports everything, and we narrow it by hand.
3Removing Split Packages and Cycles First
Split packages usually come from habits such as putting a com.acme.util package in several jars. The fix is to rename packages so each has one home, or to merge the jars. Cycles take more thought. The common technique is dependency inversion: if customer calls billing to get an invoice total, we move an interface such as InvoiceQuery into customer or into a small shared API module, have billing implement it, and wire the implementation at run time through Spring or a ServiceLoader. Now only billing depends on customer.
This step is worth doing even if we never write a module-info, because cycles and split packages are what make a monolith hard to change in the first place.
4Migrating Bottom-Up With the Class Path as a Safety Net
The order matters because named modules cannot read the class path, while class path code can read named modules. So we start with leaf jars, those that depend on nothing else of ours, such as common, then domain, then services, and give each a module-info. Everything above them stays on the class path and keeps working.
Third-party jars that are not modular go on the module path as automatic modules. An automatic module takes its name from the Automatic-Module-Name entry in its manifest, or from the jar file name if there is none, exports all its packages and reads everything. That lets our modules write requires com.fasterxml.jackson.databind even if Jackson had no module-info. Prefer libraries that declare Automatic-Module-Name, because names derived from file names can change.
- Application and web layerStill on the class path, reads everything
- Service modulesMigrated third, opens model packages to Spring
- Domain modulesMigrated second, cycles removed
- com.acme.commonMigrated first, exports only its API
- Third-party jarsAutomatic modules on the module path
5 layers, from the top down. Step through them one at a time.
Lower layers become modules first because modules cannot read code still on the class path.
Notice the opens line. Spring and Hibernate create and populate our objects by reflection, so packages they reflect into must be opened to them, or the application fails at startup with InaccessibleObjectException. This is where most of the effort goes in a Spring monolith. Also, when the main application is still on the class path, modules on the module path are not resolved automatically; we add them with --add-modules ALL-MODULE-PATH or the build tool's equivalent.
5Knowing When to Stop
We should decide the goal up front: usually it is that teams cannot use each other's internal classes and that the dependency graph stays acyclic. Once the core libraries are modules and the graph is clean, the rest of the migration, especially the top application layer with its frameworks and annotation scanning, often costs a lot and adds little.
Many teams reach the same goal with ArchUnit, a library that checks architecture rules in unit tests. A rule such as noClasses().that().resideInAPackage("..billing..").should().dependOnClassesThat().resideInAPackage("..customer.internal..") fails the build when someone crosses a boundary, with no module path, no opens clauses and no framework issues. Spring Modulith offers similar checks for Spring applications. The difference is that ArchUnit enforces boundaries at build time inside our own code, while modules also enforce them at run time and enable jlink, which matters more for libraries and runtimes than for a single deployable application.
6Follow-up questions
What happens to reflection-based frameworks inside named modules?
They need the packages they reflect into to be opened. We use opens package to framework-module for entities and injected beans, or declare the whole module open, which opens every package for reflection while still controlling compile-time access through exports. Without this, startup fails with InaccessibleObjectException.
What is the difference between an automatic module and the unnamed module?
An automatic module is a plain jar placed on the module path; it gets a name, exports everything and can be required by named modules. The unnamed module is everything on the class path; named modules cannot require it at all. So moving a third-party jar to the module path as an automatic module is what allows our new modules to depend on it.
Would you use modules or Maven modules for this?
Maven or Gradle modules give separate builds and explicit dependencies, which already prevents many cycles, and they are the first step anyway. JPMS adds hiding of internal packages inside a jar. Many teams stop at build modules plus ArchUnit and get most of the value.
7Conclusion
In this answer, we saw that modularising a monolith starts with a jdeps map, then removing split packages and cycles, which the module system forbids and which hurt the codebase anyway. We migrate bottom-up because named modules cannot read the class path, use automatic modules for third-party jars, and open packages to Spring and Hibernate for reflection. We stop when the boundaries we wanted are enforced, and we keep in mind that ArchUnit or Spring Modulith can deliver similar enforcement for an application at much lower cost.
Open with
I would start by measuring, not by writing module-info files. jdeps -summary and the package-level DOT output show which jars depend on which, where the cycles are, and which packages are split across jars.
Then, if they want more
Those two are hard blockers, because a package can live in only one module and modules cannot have cyclic dependencies, so I would fix them first, renaming packages and breaking cycles with dependency inversion. That cleanup pays off even if we stop there.
Then I would migrate bottom-up: common and domain libraries become named modules first, because a named module cannot read the class path while class path code can read modules. Third-party jars without module-info go on the module path as automatic modules, ideally ones with an Automatic-Module-Name.
In a Spring application, the big cost is reflection, so model and bean packages need opens clauses for Spring and Hibernate. I would set a clear goal at the start, typically that teams cannot use each other's internals and the graph stays acyclic, and stop when that is achieved.
For the top application layer, ArchUnit rules or Spring Modulith often give the same boundaries in tests with far less effort, so I would not push JPMS through the whole application unless we also need runtime enforcement or jlink.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are asked to modularise a large monolith's own code, not only libraries. Describe a...": https://scrivio-interview-library.netlify.app/java/modules/dd60269a93/