Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting file permission from java- Annoying issue

Posted on 2012-03-27
13
Medium Priority
?
982 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

580 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