macOS上的AccessDeniedException(Java)

时间:2016-10-05 18:06:51

标签: java macos access-denied create-directory

我尝试创建一个目录,但我经常得到AccessDeniedException。 我怎样才能访问?

代码:

String pathForMac = System.getProperty("user.home") + "Library\\Application Support\\Petatech\\Zindroid";
Path macPath = Paths.get(pathForMac);{

if (!Files.exists(macPath)) {
    try {
        Files.createDirectory(macPath);
    } catch (Exception e) {
        System.out.println(e);
        JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用Privileged Block API执行此操作。像这样的东西应该起作用,因为缺少权限,System.getProperty(String key)可能是抛出AccessDeniedException的那个。

String pathForMac = java.security.AccessController.doPrivileged(
    new java.security.PrivilegedAction<String>() {
        public String run() {
            return System.getProperty("user.home");
        }
    });
//Append the rest to the String
pathForMac += "Library\\Application Support\\Petatech\\Zindroid";