• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file reference within Jar

 
lakshminarayanan samp
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the Javaranch.

A simple question: did you check if it is actually in the jar?
 
Javad Rashidi
Ranch Hand
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but why do you use it? What is the added advantage over this.getClass().getResource("build.xml")?
 
Javad Rashidi
Ranch Hand
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic