Solved

Setting file permission from java- Annoying issue

Posted on 2012-03-27
13
927 Views
Last Modified: 2012-04-17
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
0
Comment
Question by:Sreejith22
  • 7
  • 5
13 Comments
 

Author Comment

by:Sreejith22
ID: 37771134
experts-exchange java, linux, android geeks. no replies? why?
0
 
LVL 11

Expert Comment

by:legolasthehansy
ID: 37771458
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37771794
See

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

Also - use the String[] version of exec - it's more reliable
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Sreejith22
ID: 37773335
No solid reasons yet. no solutions yet. anyone who can help with this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37774269
No solid reasons yet.

You won't see any reasons unless you handle stderr as my link describes. Have you done that yet?
0
 

Author Comment

by:Sreejith22
ID: 37775364
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37775574
I can only imagine that the original permissions are being preserved in the copying process
0
 

Author Comment

by:Sreejith22
ID: 37775888
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37776513
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?
0
 

Author Comment

by:Sreejith22
ID: 37776750
I am creating files in one application and using it in another.

While creating the file, the permission is set by default.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37777328
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
0
 

Accepted Solution

by:
Sreejith22 earned 0 total points
ID: 37841256
In android, there is no permission to share file between two applications, apart from using a Content provider. That was the only solution for this. Resolved the issue myself.
0
 

Author Closing Comment

by:Sreejith22
ID: 37855000
Investigated and resolved myself.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question