• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how can i find a file in jar

 
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have created a jar of my application.
jar contains image files also which i need to send them to report.

first i tried sending file path.but the file it self is comming null when i run jar.
but working fine when i run main class.

MyClass.class.getResource("/img/abc.png");...... did not work when running through jar.

but working with streams
InputStream inputStream = MyClass.class.getResourceAsStream("/img/abc.png");

cant i find a file in my jar??
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it quiet tough to understand, what you are trying to communicate!
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Item in a Jar/Zip-file is not really a File but a resource. A resource is found using a URL. When asking the Jar-file to get the resource that is found in file:/path/to/jarfile/my.jar!/img/abc.png you are not really getting a file but a handle to where to start reading.

If you look at the ClassLoader-class (that should be used, not a specific class) you find that the method getResource(String s) returns a URL that can then be used to get the resource. (http://download.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29)

For images, you can actually use the Toolkit-class.
reply
    Bookmark Topic Watch Topic
  • New Topic