File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
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
Author

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

Sudheer
SCWCD, SCJP 5
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

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


S.D. MADHAN
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. (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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i find a file in jar