然而,签名小程序的权限再次出现问题。
我希望我的自签名小程序从本地驱动器中删除文件,但我得到例外:
java.security.AccessControlException: access denied
(java.io.FilePermission /Users/me/tmp.txt delete)
这里我调用删除:
public void deleteFile( String path )
{
AccessController.doPrivileged( new Deleter( path ));
}
并且该类删除文件:
class Deleter implements PrivilegedAction {
public Deleter( String path ) {
m_path = path;
}
public Object run()
{
File file = new File( m_path );
file.delete();
return null;
}
private String m_path;
};
HTML代码是:
<APPLET CODE="HelloWorld.class" ARCHIVE="SignedHelloWorld.jar" WIDTH=600 HEIGHT=25>
<PARAM NAME="MAYSCRIPT" VALUE="true">
</APPLET>
解决:
我需要添加策略(在OS X下):
cat >> ~/.java.policy
grant {
permission java.io.FilePermission "<<ALL FILES>>", "delete";
};