Link to home
Start Free TrialLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

Setting file permission from java- Annoying issue

I am accessing png images from a directory using java code. The directory is created by me and the files are placed to this directory by me. There are no permissions set for the directory or files.

While I access the files, I get permission denied exception. In order to confirm, I printed the read and write permission of all the files in this directory. For few files, it has read and write permissions while for others, it returns false for read and write permissions.

In order to resolve the permission denied/file not found issue, I set the permission manually using different means.

Method 1

Runtime.getRuntime().exec("su chmod 777 " + ico); 

Open in new window


I check the read & write permission after this, it is false again!

Method 2

Process chperm;
chperm=Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(chperm.getOutputStream());
os.writeBytes("chmod 777 " + ico +"\n");
os.flush();                                  
os.writeBytes("exit\n");
os.flush();
chperm.waitFor();

Open in new window


I check the read & write permission after this, it is false again!

Can someone kindly let me know, why this happens and what is the solution I have for this. This turns out to be a literally annoying issue, as it has already eaten up some days of mine.

Any help in this regard which would lead me to the correct solution is much appreciated. I am even willing to give 500+ points for the correct solution!!

PS: All this code is executed from an android app
Avatar of Sreejith22
Sreejith22
Flag of India image

ASKER

experts-exchange java, linux, android geeks. no replies? why?
Avatar of legolasthehansy
legolasthehansy

Put it simply,

Suppose java app or binary is owned by user1:java and the java files in the directory are owned by user2:foo then 'user1' should be in group foo and the directory attributes should be r-x to access the files.
Good luck
Avatar of CEHJ
See

http://technojeeves.com/joomla/index.php/free/52-runtimeexec

Also - use the String[] version of exec - it's more reliable
No solid reasons yet. no solutions yet. anyone who can help with this?
No solid reasons yet.

You won't see any reasons unless you handle stderr as my link describes. Have you done that yet?
ok, will try that. But before that, I found one strange thing. PFA the png, in this please note the permissions for the images inside app_themes directory. I am creating and adding files to this directory(app_themes) and during the process I do not specify any permissions at all.

Now I can see, for some png's the permission is rwxrwxrwx and for some others, it is just rw.

Though I do not specify any permission at all, why this difference in permission occurs?

And I have no issues while I access a file with rwxrwxrwx from a different application.

Please let me know how to resolve this by having the same permission - rwxrwxrwx for all the files.
app-themes.png
I can only imagine that the original permissions are being preserved in the copying process
http://stackoverflow.com/questions/9903231/file-permission-issues-in-android

based on the above link, I made some detailed search which resulted in this

http://stackoverflow.com/questions/6233541/java-set-file-permissions-to-777-while-creating-a-file-object

In the second link they say 2 down vote accepted
      
If you set the umask(2) to 0 before starting the JVM, all files and directories created will be created with full permissions for everyone. This is probably a bad idea.

what does  before starting the JVM means in this context?
what does  before starting the JVM means in this context?

It means starting Java in the same shell on which a given value of umask has been set.

afaik you are not creating files as such but copying them are you not?
I am creating files in one application and using it in another.

While creating the file, the permission is set by default.
OK, then the permissions shouldn't be mixed. Anyway, you can clean them up afterwards with chmod. Do that with the code i gave you at the link
ASKER CERTIFIED SOLUTION
Avatar of Sreejith22
Sreejith22
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Investigated and resolved myself.