• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Using Policy File

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
----------------------------------------------------

IN CONSTRUCTOR
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
"http://www.colin-java.co.uk/FotoCube"
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
java.home\lib\security\java.security

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.

Thanks
[ April 20, 2007: Message edited by: colin shuker ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic