Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load one of same named resources from different jars in a classpath?

Let's say there is a jar main.jar which depends on two other jars - dep1.jar and dep2.jar. Both dependencies are in a classpath in MANIFEST.MF of main.jar. Each of dependency jars has a directory foo inside with a file bar.txt within:

dep1.jar
|
\--foo/
   |
   \--bar.txt

dep2.jar
|
\--foo/
   |
   \--bar.txt

Here is a main class of main.jar:

public class App
{
    public static void main( String[] args ) {
        ApplicationContext ctx = new StaticApplicationContext();
        Resource barResource = ctx.getResource("classpath:foo/bar.txt");
    }
}

Which of two bar.txt files will be loaded? Is there a way to specify in a resource URL a jar the file should be loaded from?

like image 313
vect Avatar asked Sep 30 '22 01:09

vect


1 Answers

Which one you get is undefined. However, you can use

Resource[] barResource = ctx.getResources("classpath*:foo/bar.txt");

to get them both (all). The URL in the Resource will tell you which jar they are in (though I don't recommend you start programming based on that information).

like image 90
Sotirios Delimanolis Avatar answered Oct 18 '22 09:10

Sotirios Delimanolis