How can Child jar access a resource present on parent Jar's classpath?

53 views Asked by At

I have parent jar. This parent jar has a resource on its classpath. This parent jar has some 3rd party dependency jar embedded in it (child jar). The child jar has some java code which tries to access a file using a path

new File(pathToParentResourceFile)

This gives a file not found exception. The file is accessible in Parent jar. If I pass this file path to child jar then it seems inaccessible.

NOTE - i cannot edit code of child JAR as it is 3rd party library.

My jar is an osgi bundle. I can access the resource in Parent Jar in multiple ways. once I get the resource, I get the path also. But when this path is used by child jar then file is not found.

1

There are 1 answers

0
Greenylie On

You should try to resolve the resource in the Parent jar before passing it to the Child jar, an example would be with URL:

import java.net.URL;

URL urlToFile = this.getClass().getResource("path/to/file");
ChildJar childJar.methodThatRequiresParentResource(urlToFile);