Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Write a module-info.java for a module com.acme.orders that uses java.sql, exposes its com.acme.orders.api package, keeps com.acme.orders.internal hidden, lets Jackson reflect on com.acme.orders.dto, and uses a PaymentProvider service.

The descriptor is: module com.acme.orders { requires java.sql; requires com.acme.pay; exports com.acme.orders.api; opens com.acme.orders.dto to com.fasterxml.jackson.databind; uses com.acme.pay.PaymentProvider; }. Packages we do not export, such as com.acme.orders.internal, are not accessible to other modules; opens ... to allows deep reflection only for Jackson; and uses declares a service we look up with ServiceLoader, which another module implements with provides ... with. The requires com.acme.pay line is needed because the service interface lives in that module, and if other modules must compile against the dto package, we add exports for it as well as opens.

Why this question is asked

Writing a module descriptor is the most direct way to show we understand the Java Platform Module System, the module system added in Java 9 that most of us use only indirectly. Each line of the descriptor answers a different question: what do we need, what do we offer, what may others look into, and which plug-ins do we use. Interviewers give this exact task because every requirement in it maps to one directive, and the details, such as qualified opens and the difference between opens and exports, separate people who have written one from people who have only read about them. In this answer, we will recap what a module descriptor is, write it line by line with the reason for each directive, see the errors when something is missing, and look at the provider side of the service.

1A Quick Recap: What a Module Descriptor Does

A module is a named group of packages with a descriptor, the file module-info.java at the root of the source tree, compiled into module-info.class. The descriptor states two things the class path never could: which other modules this module depends on, and which of its packages other modules may use. Everything not mentioned is encapsulated, which means even public classes in an unlisted package are off limits to other modules.

There are four directives we need for this task. requires declares a dependency on another module. exports makes a package's public types available to other modules at compile time and run time. opens allows deep reflection, access to private members through reflection, at run time only. uses declares that we look up implementations of a service interface through ServiceLoader.

2The Descriptor, Line by Line

Here is the complete module-info.java for com.acme.orders.

Java
// src/main/java/module-info.java
module com.acme.orders {
requires java.sql; // we use JDBC types such as DataSource and Connection
requires com.acme.pay; // the module that contains PaymentProvider
exports com.acme.orders.api; // our public API: other modules may compile against it
// com.acme.orders.internal is not mentioned: no other module can use it
opens com.acme.orders.dto // deep reflection at run time...
to com.fasterxml.jackson.databind; // ...but only for Jackson's databind module
uses com.acme.pay.PaymentProvider; // we look up implementations with ServiceLoader
}
Each requirement and its directive
RequirementDirectiveEffect
Use JDBCrequires java.sqlOur code can read java.sql types
Publish the APIexports com.acme.orders.apiCompile and run-time access to public types
Hide internal(nothing)No access from other modules, even to public classes
Let Jackson bind DTOsopens com.acme.orders.dto to ...databindPrivate-member reflection, for Jackson only
Consume PaymentProviderrequires com.acme.pay; uses ...PaymentProviderServiceLoader may find implementations

requires java.sql makes the JDBC types readable to our code; without it, even java.sql.Connection would not compile in a named module, because only java.base is read automatically. requires com.acme.pay is a line the task does not mention but the code needs: the uses directive names com.acme.pay.PaymentProvider, so our module must read the module that contains it, and that module must export the package. exports com.acme.orders.api publishes the API package. Notice that internal needs no directive at all: encapsulation is the default, and the absence of an exports line is what keeps it hidden.

3Why opens ... to for Jackson

Jackson builds objects from JSON by reflection. When a DTO has private fields, or a private constructor, Jackson must call setAccessible(true) to reach them, which is deep reflection. exports alone does not allow that, even for public classes, because since Java 9 the module system protects private members of classes in named modules unless the package is opened. Without the opens line, deserialising fails at run time.

Java
// Without the opens directive, at run time:
// java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.String
// com.acme.orders.dto.OrderDto.id accessible: module com.acme.orders does not
// "opens com.acme.orders.dto" to module com.fasterxml.jackson.databind
// (Jackson wraps it in its own exception; the cause shows this message)

The qualified form, opens ... to a named module, is the reason the task says lets Jackson reflect: it grants deep reflection to Jackson's databind module and nobody else. An unqualified opens com.acme.orders.dto would let any module reflect on the DTOs, which is more than we need. One practical note: if Jackson only uses public getters, setters and constructors, an exports to Jackson would be enough, but opens is the safe choice because DTOs often have private fields.

4What Happens When Another Module Uses internal

Suppose another module, com.acme.reports, requires com.acme.orders and imports a class from the internal package. The compiler stops it.

Java
// In module com.acme.reports
import com.acme.orders.internal.PriceCalculator;
// error: package com.acme.orders.internal is not visible
// (package com.acme.orders.internal is declared in module com.acme.orders, which does not export it)
import com.acme.orders.api.OrderService; // fine: exported

Reflection is blocked too: calling a method on PriceCalculator through reflection from another module fails with an IllegalAccessException, because the package is neither exported nor opened. This is what we mean by strong encapsulation: public no longer means visible to everyone, only to modules the package is exported to.

5Consuming the Service With ServiceLoader

uses declares that our module consumes a service, an interface whose implementations live in other modules and are discovered at run time. In the code we ask ServiceLoader for implementations.

Java
PaymentProvider provider = ServiceLoader.load(PaymentProvider.class)
.findFirst()
.orElseThrow(() -> new IllegalStateException("no PaymentProvider on the module path"));
// In the provider's module, for example com.acme.pay.card:
module com.acme.pay.card {
requires com.acme.pay;
provides com.acme.pay.PaymentProvider
with com.acme.pay.card.CardPaymentProvider; // the implementation class need not be exported
}

Notice the symmetry. The consumer says uses with the interface; the provider says provides ... with with the interface and its implementation class. The implementation package does not need to be exported, because ServiceLoader instantiates it through the module system on our behalf. Without the uses line, ServiceLoader.load in a named module throws a ServiceConfigurationError saying the module does not declare uses.

6When the DTO Package Must Be Compiled Against Too

opens gives run-time reflection only; it does not let other modules compile against the package. If our API methods return OrderDto, or another module must create DTOs, the package needs exports as well.

Java
module com.acme.orders {
requires java.sql;
requires com.acme.pay;
exports com.acme.orders.api;
exports com.acme.orders.dto; // compile-time access for everyone
opens com.acme.orders.dto to com.fasterxml.jackson.databind; // plus private reflection for Jackson
uses com.acme.pay.PaymentProvider;
}

Both lines are allowed for the same package, and they do different jobs. Similarly, if our exported API exposes JDBC types, such as a method returning java.sql.Connection, we would write requires transitive java.sql so that consumers can read java.sql without declaring it themselves.

7Follow-up questions

What if Jackson is not on the module path at all?

A qualified opens or exports to a module that is not present is not an error at run time; it simply has no effect. The compiler may warn that the module is not found, which is harmless if Jackson really is optional.

Could we write open module com.acme.orders instead?

Yes, an open module opens every package for deep reflection to every module at run time, which makes frameworks happy with no extra lines. But it gives away more than needed, including reflection on internal, so the qualified opens is the better answer to this task.

Does the implementation of PaymentProvider need a public no-argument constructor?

Either a public no-argument constructor or a public static provider() method that returns the instance. In a named module, ServiceLoader prefers the provider() method when it exists.

8Conclusion

In this answer, we wrote the descriptor for com.acme.orders: requires java.sql and the payment module, exports the api package, leaves internal unmentioned so it stays encapsulated, opens the dto package only to Jackson's databind module for deep reflection, and declares uses for PaymentProvider, which a provider module implements with provides ... with. We also saw the compile and run-time errors when a directive is missing, and that a package needing both compilation and private reflection gets both exports and opens.

What to say in the interviewAbout 70 seconds aloud

Open with

I would write module com.acme.orders with five kinds of lines. requires java.sql, because only java.base is read automatically. requires com.acme.pay, because the PaymentProvider interface lives there; the task does not say it, but the uses line needs it. exports com.acme.orders.api, which gives other modules compile-time and run-time access to the public types in that package.

Then, if they want more

For com.acme.orders.internal I write nothing; that is the point, unexported packages are encapsulated, so even public classes there are not accessible from other modules, and the compiler says the package is not visible. Then opens com.acme.orders.dto to com.fasterxml.jackson.databind.

Jackson reflects on private fields, which needs deep reflection, and exports does not allow that; opens does, and the qualified to form limits it to Jackson only, instead of opening it to everyone.

Finally uses com.acme.pay.PaymentProvider, which lets me call ServiceLoader.load to find implementations; another module provides one with provides PaymentProvider with its implementation class, and that class does not even need to be exported. And if other modules need to compile against the DTOs, I add exports com.acme.orders.dto as well, since opens is only for run-time reflection.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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.
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 "Write a module-info.java for a module com.acme.orders that uses java.sql, exposes its...": https://scrivio-interview-library.netlify.app/java/modules/e02565427b/