This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes Issues with Jar Files and loading images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Issues with Jar Files and loading images" Watch "Issues with Jar Files and loading images" New topic
Author

Issues with Jar Files and loading images

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I have a java applet on my desktop PC. The htm file that loads the applet, the applet class files, and some image files (png formt) are all in the same directory. The image files are loaded by the following method...

ImageIO.read(new File("wk.png"))

which creates a BufferedImage object. This all works flawlessly.

Question #1. I know there are security restrictions regarding applet access to the local file system. So it is not quite clear why I can do this when applets are supposed to be restricted from accessing the local file system.

Question #2. When I create a jar file using jar cvf fchess.jar * I can see that the class files and image files get added to the jar file, and when I edit my Applet tags to use the jar file everything works fine, until I remove the png files from the directory. So it follows that when you use ImageIO to load the files, it looks to the file system regardless of whether or not the image files are packed in the jar file. The problem is, I can't find another way to create BufferedImage objects from external image files.

Anyways, If someone could point me in the right direction I would be grateful.

Thanks.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

I have no clue what is that ImageIO, but I can suggest you to use Classe's getResourceAsStream method to retrieve data from your jar's resources and then create your BufferedImages based on these data.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Zandis Murāns wrote:I have no clue what is that ImageIO, but I can suggest you to use Classe's getResourceAsStream method to retrieve data from your jar's resources and then create your BufferedImages based on these data.


OK, I can see that your getResourcesAsStream method would return an InputStream object. And I can see that class ImageIO, which is in the Sun API docs, has a read method that works with an InputStream rather than directly with a file resource, so I'll be pursuing that angle, will let you know how it goes.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues with Jar Files and loading images