I am trying to implement a method that when called, get a string from a particular resource in the jar that the class is loaded from.
For example:
import mypath.myclass; //from a jar
String result = gitid.getGitId(myclass.class);
On the backed I am currently using:
InputStream is = null;
BufferedReader br = null;
String line;
is = c.getResourceAsStream("/com/file.text");
The problem is I keep getting the same resource for no matter what class I give it.
I have also tried:
is = c.getClassLoader().getResourceAsStream("/com/file.text");
This fails completely.
Any suggestions would be much appreciated.
Also,what is the difference between calling getResourceAsStream from the class loader vs the class?
The
Class.getResourceAsStream()gets aClassLoaderinstance, pretty much the same you get fromClass.getClassLoader()call.What you could do, is get the URL for a given class and replace class resource path your path of your file. for example, the following code will return resource from the same jar:
You’ll have to handle not jarred class folders separately.