I have a web application my requirement is to read some files and process it and persist the file content in database when the application starts.
class MyUtil{
/**
*Read the files
*/
public static void readFiles(){
File file = new File(ClassLoader.getSystemClassLoader().getResource("MyFile").toURI()); //NullPointerException
// ClassLoader.getSystemClassLoader().getResource("MyFile") is giving null in servlet.init() method.
if (file.isDirectory()) {
//Read all the files and persist.
}
}
}
MyFile
folder/dir is available in class path. When MyUtil.readFiles()
is called in JUnit test case it works fine. But when It's called in servelet.init()
method ClassLoader.getSystemClassLoader().getResource("MyFile")
gives the null.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…