This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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.
 
 
subject: Issues with Jar Files and loading images
 
Similar Threads
Can't "see" image inside the jar file
the pictures are not loaded into an executable jar file
Creating an applet jar file
how to load a png-file?
Where should I put the applet?