I need to look up for an image present in src/main/resources
folder in a web application. Its an Apache CXF SAOP
based web application.
We are
This is my file structure within the Maven project:
src/main/resources/
src/main/resources/META-INF
src/main/resources/adir
src/main/resources/adir/afile.json
Finally,this worked for me:
String resourceName = "adir/afile.json";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource(resourceName);
InputStream iStream = resource.openStream();
byte[] contents = iStream.readAllBytes();
System.out.println(new String(contents));
HTH, Thomas
Since you're using Maven, files from src/main/resources
will end up on the classpath automatically.
To load a resource from the classpath, use something like this:
InputStream in = getClass().getResourceAsStream("/image.jpg");
Getting the path to the resource or opening it as a File
may not always work though since the file is probably still stored inside a .war
file. class.getResource()
will therefore return a URL
that is recognizable only by the app server's classloader.