assume that i have this code in a project A
:
public class test {
public void myMethod() {
String directoryPath = classLoader.getResource("") + "/templates/code/";
// code to read directory files
}
}
First Problem
and have some specific files in resources/templates
directory of this project. and want to get all of the files that in the resources/templates
directory.
I'm added that project as a dependency to another project named B
and want to call myMethod
.
now, the myMethod
works fine when calling in project A
, but when i'm call it in B
project it throws FileNotFoundException
.
I'm call the myMethod
in project B
as you seen below:
public class myClass {
public static void main(String[] args) {
myMethod();
}
}
NOTE
1- Both of that projects is Spring, Maven project
2- When i build project B
, the jar file of project A
is in WEB-INF/lib
directory of B
.
First you could use getResource to work with files as follow :
URL directoryPath = classLoader.getResource("resources/templates");
File f = new File(directoryPath.getFile());
BUT, once your application is deployed as a jar, you can't access the ressources with "File" anymore. You need to read the content of the jar file.
Here's a link to help you : How do I list the files inside a JAR file?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.