'Is there a way to get the relative path from a jar of a jar?

I have a requirement where I want to find the path of a jar that is inside the running jar. Let's say there is a jar called example-1.0.0.jar inside a running jar called main-1.0.0.jar. I want to know the path of that jar that is present inside the running jar.

Edit 1: For example, the jar is a dependency in the project. For example, let's say I have a log4j dependency in my project and I want to know the path of the log4j jar from a class. How can I get the path?



Solution 1:[1]

A jar is just a ZIP file. So once you know how to access the 'running' jar, open it using ZipFile and scan it for the entry you like. Once you have the entry you also know the path inside the ZIP.

If by 'running jar' you mean something on the classpath you could use any classes' getClassLoader().getResource() method but in this case you'd have to know the path to the contained jar upfront.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hiran Chaudhuri