views:

78

answers:

1
grant {
     permission java.io.FilePermission "C:\\class\\*", "read, write";
     permission java.lang.RuntimePermission "createClassLoader";
};

My policy.txt

java.security.AccessControlException: access denied (java.io.FilePermission c:\class 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.isDirectory(Unknown Source)
    at java.io.File.toURI(Unknown Source)
    at loader.Main.main(Main.java:35)

My exception..???

My call

String path = "c:\\class\\";
String app = "x.MyHTMLPrint";


File file = new File(path);
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};

ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(app);

As soon as I give it full Permission it works.

any ideas? thx!

+1  A: 

There appears to be two problems there.

Firstly, as Yishai says, File.toURI appears to need to check that the file without the trailing separator is a directory. This is probably a bug.

Secondly, the wildcard for recursive subdirectories is '-' not '*'.

So your policy file needs to look like:

grant {
    permission java.io.FilePermission "C:\\class\\-", "read";
    permission java.io.FilePermission "C:\\class", "read";
    permission java.lang.RuntimePermission "createClassLoader";
};

Also, if you use URLClassLoader.newInstance, you don't need createClassLoader permissions, and you get a completed class loader implementation.

Tom Hawtin - tackline