3

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.

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • 4
    For the modules in the boot layer then this is probably close to what you are looking for `ModuleLayer.boot().modules().stream() .map(Module::getDescriptor) .filter(md -> !md.provides().isEmpty()) .forEach(md -> System.out.format("%s -> %s%n", md.name(), md.provides()));`. Keep in mind that some of the service types won't be accessible to you. It might be simpler to start with a statement on what you are trying to do. – Alan Bateman Jan 12 '18 at 09:54
  • 1
    @AlanBateman Though I would suggest you make this as an answer instead since this is, in fact, closest to getting the list of services and their providers, given a module or a module layer. Yet a question further, what exactly do you mean by *some of the service types won't be accessible to you*.. any examples? – Naman Jan 12 '18 at 10:41
  • 1
    The jdk.management modules are good example. They provide implementations of sun.management.spi.PlatformMBeanProvider, this is a JDK-internal service type. I'm sure an answer can be written once the original question is clarified as to the intent. – Alan Bateman Jan 12 '18 at 11:07
  • @AlanBateman Apologies for redirection, but it would be helpful to get some pointers from you on this question https://stackoverflow.com/questions/56147221/why-to-open-non-existing-package-from-java-module. Just bringing to attention. – Naman May 15 '19 at 13:41

2 Answers2

2

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.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • Thank you for nice answer. But could you add information how to do it via code? I asked in running JVM - I meant via code. – Pavel_K Jan 12 '18 at 06:35
2

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()));
Pavel_K
  • 10,748
  • 13
  • 73
  • 186