Reputation: 951
I have the following problem: I try to load a file from jar but I have the same file outside situated at the same relative path: "res/rtrconfig" as the file in jar. How can I ignore the outside file and get the jar file path?. I can't delete the outside file because other parts of my code need it. What I've got until now:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(path);
//check if file is in jar or not
if (url == null) {
System.err.println("FATAL: Properties file not found.Stopping the server.");
System.exit(-1);
}
if ( !url.getProtocol().equals("jar")){
System.err.println("Properties found outside jars.Please delete " + url.getPath() + " and re-start the server");
System.exit(-1);
}
This part of code checks if the file is founded in jar or in path. I don't know how to configure URL to look after file in jar before file in path. Thanks for your time.
Upvotes: 0
Views: 579
Reputation: 1
If you are using spring this will work to get the jar resource file from outside jar.
ClassPathResource resource = new ClassPathResource("schema/feed.xsd");
Upvotes: 0
Reputation: 136162
You can get all resources
Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources(path);
file URL will have "file" protocol
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url.getProtocol().equals("file")) {
...
}
}
Upvotes: 1