1

I have a piece of Java that create folder(s) on a network mapped drive (Z:) Script executes on server A (Windows 2008 R2, running as user account "serviceUser") and creates folder(s) on server B (Windows 2003)

The root folder on server B (which is mapped as Z:) has special permission and allows "serviceUser" to create, modify, delete, write, etc. Permissions are set to inherit to child object, so folder created in Z: should get the same permissions as Z: itself.

My code creates 2 folders inside Z: like:

File destination = new File("z:\\folder_1\\");
File destination = new File("z:\\folder_1\\subfolder_1\\");

Folder "folder_1" gets the correct permissions but "subfolder_1" does not. After creating those folders, I need to create a file, but as the "subfolder_1" doesn't get permissions, console reports "Access is denied" when doing File fileName = new File("z:\\folder_1\\subfolder_1\\filename.png");

How can I fix this problem?

skaffman
  • 398,947
  • 96
  • 818
  • 769
user706058
  • 415
  • 3
  • 11
  • 23

3 Answers3

1

Have you tried the mkdirs command? It will delegate to the OS to create all needed directories in your overall path.

File destination = new File("z:\\folder_1\\subfolder_1");
destination.mkdirs();
Perception
  • 79,279
  • 19
  • 185
  • 195
  • wouldn't it be better if you first check if the directory exists or not? `if(!destination.exists()) destination.mkdir();` – Eng.Fouad Jul 09 '11 at 16:34
  • I don't think that is a scenario that is occurring for the OP, but there is absolutely nothing wrong with doing that pre-check. The mkdirs call ignores any pre-existing directories in the given path. – Perception Jul 09 '11 at 16:39
  • @Perception thanks, I use that do avoid calling the method for each folder, but it doesn't solve the permission issue, unfortunately. – user706058 Jul 09 '11 at 16:41
  • @user706058 - check the properties of folder_1 after its created. Are the inherit permissions intact on it? – Perception Jul 09 '11 at 16:57
  • @Perception - yes, permission gets inherited to `folder_1` but not to `subfolder_1` – user706058 Jul 09 '11 at 17:34
1

Try to use: setReadable() and/or setWritable() on your folder2. This is the only portable pure java way to control file permissions.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • I just tried, same issue with Access is denied message when doing: `File destination = new File(FolderPath); destination.setReadable(true, false); destination.setWritable(true, false);` – user706058 Jul 09 '11 at 16:40
0

The only successfull way I found to make this work was to set the user account as an administrator of Server B with full control.

user706058
  • 415
  • 3
  • 11
  • 23