This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am trying to refer a XML file within a jar using this.getClass().getResource("build.xml").toURI(). build.xml is in same package level as Class file. When I try to execute the code from eclipse, it works fine. But from jar, it gives following error rsrc:com/build.xml URI is not hierarchical..
any ideas?? If you want to see folder structure or source code let me know.
@Javad: can you tell us why that approach should be used? Because I don't see the added value. getResource() delegates the loading to the classloader which is what you are basically doing only with a reference to a swing object (and don't use any specific swing methods, only methods from Object).
i usually use this method for loading image from jar to my components. i also test it for .xml .txt .doc .wav .mp3 .class and it works well. you can change type of parent to Object.
i had problem with getResourceAsStream in one of my project and then i use URLClassLoader.
i had a problem in one of my project, getResource didn't work and when i use URLClassLoader the problem solved. i think there is no advantage over getResource they almost work like each other. i didn't know what's the cause of my problem but since i use URLClassLoader i don't get any problem for loading file from jar.
@Javad: Just to add some information that is also going to work the same way because URLClassLoader and Object both are bootstrap classes and both are loaded by Bootstrap class loader. So, replacing with above code should also work.
@Wouter: Correct me if i am wrong; may be will differ from Javad's in the sense that with your's it will ask AppClassLoader to load the resource and in Javad's case it will ask Bootstrap class loader. And this should be useful as child loader (AppClassLoader) will have visibility into classes/resources loaded by parent class loader (Bootstrap class loader).