This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i find a file in jar

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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??
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it quiet tough to understand, what you are trying to communicate!
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic