How to use FilePermission class to change the file permission in Linux

Hi all,

I'm running in a issue related to changing the file permission under Linux environment. I'm using Suse 10.0 Linux and run jdk 1.5 java runtime.

I want to create a file during execution time and change the permission of the same. I use File.createNewFile() API to create file. By default it does not have all the 3 permission (read,write,execute) for all the user ( user,owner,group). Through program I have to change the file permission ( equal to execute ,chmod 777 filename).I donot want to use runtime.exec() with chmod as the argument , since it creates a process to change the permission which may impact the performance. I want to try FilePermission class , by giving filename and permission as the argument. But it is not changing the permission . Could any one faced this problem ?

If any body guides me, how to change the permission of the file under Linux using FilePermission class , it would be helpful.

Thanks,

Sankar

[978 byte] By [Sankar_Gandhia] at [2007-11-26 18:53:33]
# 1
FilePermission has nothing to do with the permissions in the filesystem. It used by the java SecurityManager only.But JDK 1.6 should provide what you are looking for. In 1.6 java.io.File has methods like setExecuteable(boolean), setWriteable(boolean),....
ekupcika at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 2

In Java 1.5 I know of only two solutions. One, as you say, is to exec chmod. That will indeed fork a process. The alternative is to use JNI and invoke the chmod() system call yourself. JNI also has some overhead, but presumably less than a fork. Try out both, and let us know which is faster...

glevnera at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 3
As a general solution, you could set your umask in an appropriate way prior to starting the java program, if it is okay that all files will be created with that one permission set.
BIJ001a at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for reply ekupcik ,Can I confirm , there is no alternative method in JDK 1.5 to change the file permission other than Runtime.exec() method.In that case I will try to optimize the ovrehead in causing in Runtime.exec().
Sankar_Gandhia at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks glevner,I will let you peoples know the performance comparision of both method
Sankar_Gandhia at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks BIJ001 ,How do I set the Umask prior to starting the program ? Could pls explain in steps.
Sankar_Gandhia at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 7

How do I set the Umask prior to starting the program

> ? Could pls explain in steps.

I don't think umask can help you. The mode mask prevents permission bits from being set when a file is created, but does not force any bits to be set. So if execute permission is not set explicitly when a file is created, umask will do nothing about it.

But if you want more information about umask, see the man page for your shell script (sh, csh, etc.) or "man 2 umask".

glevnera at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...
# 8

So tell us, you are writing a program which creates scripts or copies executable binaries. Otherwise you wouldn't be setting the 'x' permission at all. So how many of these is your program going to create per second and how much impact is the cost of Runtime.exec() going to have? Given that there is no alternative?

ejpa at 2007-7-9 6:27:34 > top of Java-index,Java Essentials,Java Programming...