user5182503
user5182503

Reputation:

How to list all JPMS services?

How to list all(!) JPMS services of all modules. I mean I need the list of the all services that can be used at the current moment in running JVM. I've searched in Internet but still can not find a way.

Upvotes: 3

Views: 370

Answers (2)

user5182503
user5182503

Reputation:

The answer was given by Alan Bateman in comments.

ModuleLayer.boot()
    .modules()
    .stream() 
    .map(Module::getDescriptor) 
    .filter(md -> !md.provides().isEmpty()) 
    .forEach(md -> System.out.format("%s -> %s%n", md.name(), md.provides()));

Upvotes: 2

Nicolai Parlog
Nicolai Parlog

Reputation: 51130

There is no such command, but you can put something together in the command line to achieve it. Here's how to do it on Linux.

First create a file with all module descriptors. (You don't actually have to, you could pipe it into the rest, but that part takes time and you might need them more often, so it makes sense to store them.)

java --list-modules
    | sed -e 's/@.*//'
    | xargs -n1 java --describe-module
    > module-descriptors

This lists all modules, removes the version strings and then lets Java describe each module, which includes the modules descriptor (and hence used and provided services).

Next step is to grep only the uses directives, remove the uses part, and sort the remaining type names while removing duplicates:

cat /opt/jdk-9/module-descriptors
    | grep "uses"
    | sed 's/uses //'
    | sort -u

I've put the output into a gist.

Upvotes: 1

Related Questions