I have created an applet within a web page, but whenever I run it, I get this:
Exception in thread "Thread-13" java.security.AccessControlException: access denied ("java.io.FilePermission" "defensebg.png" "read")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.File.canRead(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at Defense.run(Defense.java:63)
at java.lang.Thread.run(Unknown Source)
How do I fix this?
Since I know a little about your applet and am convinced these images are an inherent part of the application, I will take a different tack to paulms4.
Forget the
Fileinstances. They are neither workable for this, nor necessary. Only a trusted applet can access aFile, but even then, the only place that an applet can establish aFileis one that points to locations on the file-system of the user’s PC. Obviously the images for your applet are not available that way (OK – they might be in a browser cache, but that is no use to us).It would be more typical (& easier) to access applet resource by
URL. An URL can be established relative to the code-base or document-base of the applet. If the images are actually inside a Jar, they become an embedded resource – see the info. page as to how to gain an URL.Most of the methods in the J2SE that load a ‘read only’ resource will accept
File,URLorInputStream. I use URL most often for the generic utility of it. An URL can represent a web resource, a file on the local file system, or a resource buried deep inside a Jar file (whether or on the web or the local file-system).