This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
No, you need to look on the classpath. Assuming your Jar is on the classpath, and the file is in the com/mypackage directory of the jar, you can do something like:Note that it's been a while since I've done this and my comments are from memory... I just wanted to point out that you're looking on the classpath, not in the file system.
Joined: Dec 03, 2009
I think ServletContext.getResourceAsStream() is more safe to use, since its servlet container api. And question was about this method.
Beside that, if i have 2 jar files in my lib wich have same files, and i must get them, what should i do... in this case a cant use class loaders because this files may not have any classes at all.
In scwcd book i've read that this method supports jar files, but i can't find any information how to work with jars this fashion.
Mark E Hansen
Joined: Apr 01, 2009
As far as I know, getResourceAsStream is pulling stuff via the classpath. It doesn't matter if what you're looking for is a class file or not. For example, if you have a Jar file on the classpath of the application, and it contains a file named myfile.txt in the root of the Jar, you get get it using getResourceAsStream("myfile.txt").
If you have two Jar files on the classpath and they both contain the same file, it will get which ever one is first on the classpath. Basically, don't do this.