Reputation: 1380
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
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