tags:

views:

353

answers:

3

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?

A: 

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.

Java 6 SE File Class Doc.

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()?

Rob Lachlan
A: 

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().

David Zaslavsky
+1  A: 

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.

sateesh