How to tell which module does the package belong to?

Viewed 169

Given some package foo.bar, how do I find which module does it belong to?

For example, package java.util belongs to module java.base. I can verify it by typing in jdeps -m java.base and by going through the long output. This however assumes my prior knowledge that package java.util belongs to module java.base.

How can I verify which module does the package belong to?

1 Answers

Is this what you want?

Optional<Module> found = ModuleLayer
        .boot()
        .modules()
        .stream()
        .filter(module -> module.getPackages().contains("java.util"))
        .findFirst();
Related