I create a folder in my java program (running on linux) with mkdirs() function of File object. The problem is that the folder gets only read permissions for the group. I need it to have also write permissions (in order to be able to delete its files later). What is the way to do it?
5 Answers
A very simple solution for this is:
file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(true, false);
In above code, file is a File object.
When you are creating a file set these permissions to a file setExecutable(true)
will allow you to set that file as Executable
for owner only. If you add one more parameter which I have added in above code file.setExecutable(true, false);
will make Executable
false
for owner only, that means it will set permissions to all group / world.
Tested and working on Debian and Windows XP.

- 26,765
- 9
- 65
- 71

- 199
- 1
- 3
-
2a better solution is using java nio because this solution sets permissions for a owner only or for all. – Nurlan Feb 10 '16 at 09:56
-
1This doesn't work for me. I tried to create dir under root folder (on linux) with java application (user is tomcat) but it fail even if I use `if (!rootFile.canWrite()) { rootFile.setWritable(true, true); } ` seems to me that tomcat user cannot change permission on dir/files – pikimota Mar 16 '17 at 15:50
-
1The question is about giving permission to group, not to all! – 9ilsdx 9rvj 0lo Dec 11 '18 at 14:08
Java nio can help with posix file attributes: https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributeView.html
Path path = ...
Set<PosixFilePermission> perms =
EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE);
Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
-
2PosixFilePermission is only for the Portable operating systems. PosixFilePermission wont work with the windows systems – Baji Shaik Oct 19 '18 at 12:22
I suspect (unless/until someone posts an answer to the contrary) that there's no way to do this in Java's standard library, because POSIX group permissions (the rwxrwxrwx kind you're used to) are not cross-platform. Java 6 will let you set owner permissions or global permissions, but (as far as I can tell) not group permissions. If you really must do it, try using Runtime.exec("chmod g+w directory")
, but it might be a good idea stylistically to wrap it in a method like setGroupWritable()
.

- 128,184
- 27
- 255
- 279
-
1It is not nice, but does the work. Just for Copy/Paste: It has to be Runtime.getRuntime.exec("chmod g+w directory") – schoenk Sep 23 '16 at 07:08
-
@schoenk - `Runtime.getRuntime().exec("chmod g+w directory);` ? – Jason Doucette Apr 22 '19 at 18:43
OK this is not a java solution and definitely not portable.
Since you mention that you are linux, probably you can think of checking the "umask" settings and setting it appropriately (to have directories created with group write permissions) and then launching your java program.

- 27,947
- 7
- 36
- 45
On java 6, there are methods that allow you to do this, like setwriteable(). On previous versions of java, you'll have to access the command line to do a chmod command.
EDIT: Woops, I'm completely wrong; I failed to notice that you wanted group permissions specifically. Those don't appear to be settable without Runtime.exec().
@David: You're right.
Another thought: if you have a lot of files to change, how about writing a shell script and calling that from runtime.exec()?

- 14,289
- 5
- 49
- 99