Solved

Setting file permission from java- Annoying issue

Posted on 2012-03-27
13
908 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
Comment Utility
experts-exchange java, linux, android geeks. no replies? why?
0
 
LVL 11

Expert Comment

by:legolasthehansy
Comment Utility
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
Comment Utility
See

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

Also - use the String[] version of exec - it's more reliable
0
 

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

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

Author Comment

by:Sreejith22
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Investigated and resolved myself.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now