GeeCON Prague 2014*
The moose likes Applets and the fly likes Exception loading images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "Exception loading images" Watch "Exception loading images" New topic
Author

Exception loading images

Chris Robinson
Greenhorn

Joined: Jan 21, 2005
Posts: 6
I have built an applet that has a few buttons on it and everything was working fine. However, when I tried changing the buttons to use icons made from gif images I get something like:

AccessControlException: access denied (java.io.FilePermission ..... read)

Now, I understand that this is a security issue to protect the clients system from malicious access to their machine but what I don't get is that I am trying to access images that are in the same directory as the applet classes, not client files. I don't want to create or change any files on the local machine. Is there a simple way around this by using certain pathname conventions or something. At the moment I am just using the image filename since all the files are in one directory.
Any help greatly appreciated.
Chris.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

You are trying to read the files from the local file system. Imagine your client is on one computer and your web server is on another. How would you find the image files when they're on the server's file system?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Chris Robinson
Greenhorn

Joined: Jan 21, 2005
Posts: 6
Originally posted by Joe Ess:
Imagine your client is on one computer and your web server is on another. How would you find the image files when they're on the server's file system?


This is why I don't understand the problem. If the client downloads an applet class from the server then it must know the address that it came from. Therefore if I keep all the image files in the same directory as the applet class why should there be a problem if I try to reference the files using purely the filename and no path?
I also tried using the full URL of the image on the server but then I get a Socket Permission error at the target URL.
I just want to put my own icons on some buttons on my applet. Why is there a security issue trying to do this and what is the easiest way to rectify the problem?
[ March 28, 2005: Message edited by: Chris Robinson ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Originally posted by Chris Robinson:

This is why I don't understand the problem. If the client downloads an applet class from the server then it must know the address that it came from.


You can FIND the address it came from (through Applet.getDocumentBase() or Applet.getCodeBase()) but that doesn't mean that all file system requests automatically get converted into network requests. Code does what you tell it to do. A FileInputStream ALWAYS accesses the file system. If you construct a URL you have to get the host address that you want to access and feed it to the URL constructor. Applets are only permitted to contact the host they were downloaded from, so plugging anything else in there will cause a security exception. If this doesn't make sense, share a sample of your code trying to load an image from a URL and we'll work through it.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In your applet code, did you use getImage or ImageIO?
Chris Robinson
Greenhorn

Joined: Jan 21, 2005
Posts: 6
Thanks guys. Problem solved.
I changed my code to use Applet.getCodeBase() to create the URL object as you suggested and it all works fine.
Thanks for your help.
Chris.
 
GeeCON Prague 2014
 
subject: Exception loading images