?
Solved

Setting file permission from java- Annoying issue

Posted on 2012-03-27
13
Medium Priority
?
955 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 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