java.security.AccessControlException: Access denied (java.io.FilePermission

橙三吉。 提交于 2019-11-26 09:54:33

问题


final File parentDir = new File(\"S:\\\\PDSPopulatingProgram\");
parentDir.mkdir();
final String hash = \"popupateData\";
final String fileName = hash + \".txt\";
final File file = new File(parentDir, fileName);
file.createNewFile(); // Creates file PDSPopulatingProgram/popupateData.txt

I am trying to create a file in a folder but I am getting exception as

java.security.AccessControlException: Access denied

I am working in windows environment. I can create a folder from the Windows Explorer, but not from the Java Code.

How can I resolve this issue?


回答1:


Within your <jre location>\lib\security\java.policy try adding:

grant { permission java.security.AllPermission; };

And see if it allows you. If so, you will have to add more granular permissions.

See:

Java 8 Documentation for java.policy files

and

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html




回答2:


Although it is not recommended, but if you really want to let your web application access a folder outside its deployment directory. You need to add following permission in java.policy file (path is as in the reply of Petey B)

permission java.io.FilePermission "your folder path", "write"

In your case it would be

permission java.io.FilePermission "S:/PDSPopulatingProgram/-", "write"

Here /- means any files or sub-folders inside this folder.

Warning: But by doing this, you are inviting some security risk.



来源:https://stackoverflow.com/questions/10454037/java-security-accesscontrolexception-access-denied-java-io-filepermission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!