• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

file reference within Jar

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and welcome to the Javaranch.

A simple question: did you check if it is actually in the jar?
 
Ranch Hand
Posts: 34
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic