2016-10-05 4 views
0

Я пытаюсь создать каталог, но я постоянно получаю AccessDeniedException. Как я могу получить доступ?AccessDeniedException на macOS (Java)

код:

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); 
     } 
    } 
} 

ответ

0

Вы можете использовать Привилегированный блок 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"; 
+0

На самом деле, Paths.get (pathForMac), Files.exists (macPath) и Files.createDirectory (macPath) может бросить SecurityException. Проверьте трассировку стека и заверните соответствующие операторы в doPrivileged. – Harold