In my multi module maven project ,learning_jaxrs (parent project)
commonapi module(packaging jar) contains resources in src/main/resources ,and i am reading files using my below util class
public class FileUtils {
private static final ClassLoader CLASS_LOADER = FileUtils.class.getClassLoader();
/**
* @param fileName
* @return
* @throws IOException
*/
public static String readFileAsString(String fileName) throws IOException {
String result = null;
result = new String(readAllBytes(getFilePath(fileName)),StandardCharsets.UTF_8);
return result;
}
/**
* It will return fully qualified path of resource file name
*
* @param fileName
* @return
*/
public static Path getFilePath(String fileName) {
Path result = null;
URL resource = CLASS_LOADER.getResource(fileName);
if (resource != null) {
result = get(resource.getFile());
}
return result;
}
}
reading file
String jsonTestData = readFileAsString("testEmployees.json");
when i ran local tests for commonapi module everything works fine .
But its not working when i deployed httpmethods module(war) on my server ,i am getting following exception ..
java.nio.file.NoSuchFileException: /content/httpmethods-1.0.war/WEB-INF/lib/commonapi-1.0.jar/testEmployees.json
httpmethods war archive does contain commonapi jar in web-inf/lib and common api jar has file too
Edit :
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
solved the problem