javaimageappletappletviewer

Loading images in Java applet AccessControlError


I'm writing an applet that has to load images. When I run it through appletviewer everything works fine, so my code should be all set. When I try to open it in any browser though, I get the AccessControlException error saying I don't have read permission. I understand that normally this is because the applet is trying to access files on a client's computer. My .Java, .class, and .html files are in C:/Java and the images I need to load are in C:/Java/Images so I thought they should be accessible, am I wrong? Is there any way to get my applet to load these images in a simple way?


Solution

  • An applet running in a browser doesn't have permission to open a file from the local filesystem.

    You'll need to add the images to the jar file, and then use getClass().getResource() or getClass().getResourceAsStream() to load it.

    Edit: Here is an example showing how to load images in an applet.