File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how can i find a file in 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 "how can i find a file in jar" Watch "how can i find a file in jar" New topic

how can i find a file in jar

sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

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??

Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

I find it quiet tough to understand, what you are trying to communicate!

Not many get the right opportunity !
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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. (

For images, you can actually use the Toolkit-class.
I agree. Here's the link:
subject: how can i find a file in jar
It's not a secret anymore!