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?
Asked
Active
Viewed 332 times
1
1 Answers
1
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.

GreyBeardedGeek
- 29,460
- 2
- 47
- 67
-
*"You'll need to add the images to the jar file"* Or the 'home server'. – Andrew Thompson Nov 29 '12 at 01:58
-
Yes, adding them to a server for the applet to communicate with would certainly work, but is probably more than the OP wants to do - and I'm *guessing* that he or she is opening the html file from the file system, not using a server. – GreyBeardedGeek Nov 29 '12 at 02:04
-
1You make it sound hard! `URL url = new URL(getDocumentBase(), "image42.jpg");` will form an URL to an image in the same directory as the HTML. – Andrew Thompson Nov 29 '12 at 02:09
-
1+1 : Obviously, it's been some time since I wrote an applet :-/ – GreyBeardedGeek Nov 29 '12 at 02:11
-
*"some time since I wrote an applet"* That is something I would be rejoicing. ;) – Andrew Thompson Nov 29 '12 at 02:20
-
yes I am opening the html file from the file system. I'm just starting out with Java, so I don't know much about what I'm doing, but let me explain what I need to do with the pictures. I'm loading 20 pictures and placing them on buttons. Right now I'm using ImageIcon to do this, but it doesn't seem to like when i try to combing URL with ImageIcon. any other suggestions? – Deej Nov 29 '12 at 21:24
-
thanks guys, i'll try adding the pictures to a jar file and loading them from there. i read a little about that and it definitely seems to answer my problem. – Deej Dec 04 '12 at 01:15