Images not loading as a security issue?
I'm having a problem with getting an applet to work on the internet. I posted the applet on a web page, and when the applet initializes, it attempts to load images from a subdirectory of the web site. Basically the structure of the website is like this:
Main Directory
index.htm
images (subdirectory)
ship1.gif
ship2.gif
etc.
And the index.htm file attempts to do stuff like
Image ship1 = new ImageIcon ("images/ship1.gif").getImage ();
And I get a security violation for doing that. Why? I'm loading stuff from a subdirectory of where the applet originated from.
This is the exact error in the Java Console:
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission ship1.gif read)
Can anyone help me out?
Yep, that's right. A Java applet by default has NO permission to access the local filesystem by default. It also won't be allowed to open a network connection to any server but the one from which the applet originated.
To change this, the JRE java.policy file located in JRE_HOME/lib/security will need to be updated to allow your applet to run.
grant codeBase "http://www.example.com/path/to/applet.jar" {
permission java.io.FilePermission "/path/to/file" read
}
Obviously, this will require you to have access to the user's local machine. If this isn't an option, you will have to rethink your design.
No, you're loading from a directory on the client system.
Now it may happen that you loaded the applet from that directory because you're just testing on the server, but that isn't the point. In real life the applet is going to be running somewhere else. (Or if it isn't, you're wasting your time writing an applet. Just write an application.)
Don't any of the applet tutorials have examples of loading images? I would be surprised if they don't. Basically you have to load them from a URL (which points to your server) and not from a file.