Misc

Resource Loading

Use either of below way given a) directory is on the classpath. b) class loading the resource is loaded by the same classloader.

1.    From ClassLoader perspective, all paths are “absolute” already – there’s no context from which they could be relative. Therefore, no need for a leading slash.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(“SomeTextFile.txt”);

2.    From Class perspective, the path is relative to the package of the class unless a leading slash is included, so if we don’t want to use the current package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream(“/SomeTextFile.txt”);

Advertisements