Catanzaro
Catanzaro

Reputation: 1380

How list all package in a specific jar file?

How can I get all package names in a specific jar file from within Java?

I know find how to get all classes in a jar, but how do I get only package names?

Upvotes: 0

Views: 886

Answers (1)

lczapski
lczapski

Reputation: 4120

If you want do this in java code then you can use JarFile.

List all classes from given jar and from that get all packages (by extracting its package from full name).

JarFile jar = ... \\ create from file

jar.stream()
    .map(ZipEntry::getName)
    .filter(name -> name.endsWith(".class"))
    .map(name -> name
        .substring(0, name.lastIndexOf('/'))
        .replace('/', '.')
    )
    .distinct()
    .forEach(System.out::println);

Upvotes: 1

Related Questions