aspose file tools*
The moose likes Java in General and the fly likes file reference within Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "file reference within Jar" Watch "file reference within Jar" New topic
Author

file reference within Jar

lakshminarayanan samp
Greenhorn

Joined: Feb 15, 2011
Posts: 10
Hi,

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.

thanks
Valery Lezhebokov
Ranch Hand

Joined: Jun 12, 2006
Posts: 39
Hi,

what do you want to do with the file? If you need to read it, then I suggest to use this.getClass().getResourceAsStream(fileName) for that purpose.

Hope this helps,
V.

SCJP 1.5, OCE EJB 3.x
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi and welcome to the Javaranch.

A simple question: did you check if it is actually in the jar?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

Hi lakshminarayanan
use this

parent: component that want load file to it.
path: path of file from root of jar file.
this method convert the path of file that in jar to URL that is very usefull for me.
hope this help


http://www.myviwo.com
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

@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).
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes but why do you use it? What is the added advantage over this.getClass().getResource("build.xml")?
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

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.
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

@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).

Regards,
Himanshu
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I believe that that is correct. I mostly was interested in the explanation why you would need a JComponent reference and not just a system class reference.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: file reference within Jar