jQuery in Action, 3rd edition
The moose likes Applets and the fly likes Using Policy File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Using Policy File" Watch "Using Policy File" New topic

Using Policy File

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
Hi, I have an applet, it worked fine, until, I decided to look at the local filesystem from inside the applet.

The exception comes from using File.list() method, used so that I can get the subdirectory names of the "images" directory, which resides in the same directory as applet on the server.

Here is the output from the Sun Java Console verifying the File.list() as the place where it has an exception

Java Plug-in 1.6.0_01
Using JRE version 1.6.0_01 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Colin

c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>

java.security.AccessControlException: access denied (java.io.FilePermission images 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.list(Unknown Source)
at foto.Directory.setEndDirectory(Directory.java:75)
at foto.Directory.<init>(Directory.java:25)
at foto.Main.start(Main.java:57)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Since all I need to do is read names of directories I thought it might be possible to not use a signed applet, and just use a Policy File thing.

I read Javaranch's FAQ, and it says I can use...

So for the codeBase, I replaced it with
This is the directory where the JAR file and HTML page reside.

I'm using Eclipse, which creates a java.policy.applet file, similar to the code above, so I put the new code in, and exported it with the JAR.

But I'm still getting exactly the same exception when I run the applet in the browser.

Do I have the codeBase line correct, or is the java.policy.applet code wrong, or am I doing something else wrong?

Please can someone advise me so I can read names of subdirectories inside the applet.

Thanks for any help.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
How did you install the policy file? Here is the section of the Java Tutorial that talks about how to tell the JVM that there is a new/additional policy file.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I didn't create it, it just appeared appeared in eclipse underneath the package with all my classes in.

This is so confusing, I don't think I'll ever get it to work.
Any ideas
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I just used the policytool command, which creates a policy file.
I opened it with notepad, and its pretty much the same as the file that Eclipse creates with the applet...

grant codeBase ....

etc. etc.
So when I export the project into a JAR, I have the option to tick the
java.policy.applet, I have tried using...

grant codeBase "http://www.colin-java.co.uk/FotoCube/-" .....

Instead of

grant codeBase "http://www.colin-java.co.uk/FotoCube" .....

But still doesn't work, the precise policy file is...

grant codeBase "http://www.colin-java.co.uk/FotoCube/-" {
permission java.io.FilePermission "<<ALL FILES>>", "read, write, execute, delete";
permission java.net.SocketPermission "*", "accept, connect, listen, resolve";
permission java.util.PropertyPermission "*", "read, write";
permission java.lang.RuntimePermission "*";
permission java.awt.AWTPermission "showWindowWithoutWarningBanner";

So this has all file permissions, and it still doesn't work, what else can I do??
Thanks for any help.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
If you follow the link I posted you'll see how to install the file so that the JRE knows about it. Policy files don't just get picked up by being in the same place as an applet.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I've read it, and I'm able to follow the instructions using policytool, but after that it doesn't make any sense.
Whats the point of Eclipses' java.policy.applet file if it doesn't work?

I've looked at Approach 2 of "See the Policy File Effects",
And I've managed to locate the Security Properties File at

But what is 'policy.url.n' and where is it?

Ok, I've just edited this post, I got my JAR signed, and it works!
I used keytool, then jarsigner from command prompt.
This overwrites the jar file, and makes it work.

Not sure if I need the policy file inside the jar now or not.
But I can play around with it.

[ April 20, 2007: Message edited by: colin shuker ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
The policy file is not needed if the applet is signed, and not used in any case. Eclipse probably creates it to help the developer make the appropriate changes to the system policy file. The link I posted talks about how to do that, and what "policy.url.n" means.
I agree. Here's the link: http://aspose.com/file-tools
subject: Using Policy File
It's not a secret anymore!