0

This is the directory structure of an applet project I've created.

Project
|____classes
|____src
|____resources
  • The applet is in the src directory.
  • The classes are stored in the classes directory.
  • All the images, sounds and other stuff are stored in the resources directory.

My problem is that when I try to load an image stored in the resources directory into my applet (which is in the classes directory when compiled), the JVM raises an AccessControlException. I read a lot of posts in stackoverflow about this, and now I understand why it is like this (to protect the user from accessing his files). I also read that you can write policy files to fix it or signed jars.

This is how I load my image:

Image image = getImage(getCodeBase(),"path/to/the/image/image.png");

I noticed that AccesControlException is not raised when the image I want to load is in one of the classes sub-directory. So I can't use .. to access the classes parent directory, and then the resources directory. Is there any way I can load my images without having to put them in a classes subdirectory and without using policy files or signed jars?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Patrick B.
  • 1,257
  • 10
  • 18

1 Answers1

1

"I can't use .. to access the classes parent directory, and then the resources directory.

Why not? Always worked for me. See for example this applet. Unfortunately it is trusted (to allow it to reach cross-site), but if you are running an Oracle JRE, you should be able to Cancel the trust request to see something like..

Accessing an URL from same site

In this applet:

The bottom line is, an applet should be able to get resources from anywhere on the home server for which the server will provide resources.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you for your reply. Everytime I try to access a "classes" parent directory, oracle jvm throws an AccessControlException. Perhaps it as something to do with the fact that I don't run my applet in a browser. Instead, I use the appletviewer command-line in msdos. – Patrick B. Dec 14 '12 at 14:15
  • 1
    Aha! That **is** the problem. The applet viewer used to have no sand-box, but now it does, it is even tighter than on a web site for a fairly simple reason. The `SecurityManager` cannot tell from a file system where the 'root' of the site is - so it takes a precautionary measure of only allowing sub-directories to be considered 'home site'. At least, that is the best I could figure from working with it. It is time to test in a browser, off a local server. – Andrew Thompson Dec 14 '12 at 14:18
  • You're rigth. I tested my applet in a browser and I can access "classes" parent directory. Thank you for your help! – Patrick B. Dec 14 '12 at 16:50