This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Security Exception for showing images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Security Exception for showing images" Watch "Security Exception for showing images" New topic

Security Exception for showing images

Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Hi, i am trying to make an applet to show simple images. The code compiles fine but the applet won't load up in my browser (IE5.5) and displays the following in my Java console.

The exception is shown below

I have read up a bit about Security Exceptions but cannot seem to find what i need to get this program to work.
I will be working with applets to retrieve data and images from a database / server files quite a bit and need to look up this topic quite well so is there also anything out there (good sites) that will help me out apart from the Sun documents.
[ May 10, 2002: Message edited by: Sam Tilley ]

Sam Tilley SCJP, SCWCD
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
The default behavior of the JVM "sandbox" is to prohibit the applet from being able to load files from the local file system. This is to prevent malicious code from examining the contents of a user's hard drive, and perhaps sending files to some server (stealing them, etc.)
You can load images from the web server to which you are connected. If you really need to load local files, you'll have to change the security policy for the applet to allow this.

SCJP 1.4
Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Cheers Rob, so i need to post my applet and gifs up to my website for example and operate it from there???
My problem remains that to develop code to make it work i have to post it to my server each time and this could be quite a long winded way of doing it... But then i suppose if it keeps it safe it should work ok
Bit of trial and error needed i feel
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
For strictly testing purposes you can either run your applet in AppletViwer, which has all security permissions turned on by default, or create a testing security profile which allows your applet to load files from the file system. This can simplify your edit-compile-run cycle, but you have to understand it won't work when you deploy your applet to the "world."
You can try to reference all your gifs as relative to your document base (getDocumentBase()), because this uses a URL and only loads the files from your web server, which is allowed.
You can also get your images as a resource (see Classloader.getResource()), and if you have packaged all your needed gifs in the same classpath along with your .class files, those gifs are then available to your applet since they're downloaded along with your .class files (embedded in the jar file).
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Don't forget, you could just install a very simple and free server on your development machine for testing purposes.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Security Exception for showing images
Similar Threads
Buttons with imagens in applet from server
AccessControlException and DefClassNotFound
Unable to show icons in toolbar in the applet
custom classloader in applet
Applet problems