0

I am using Window's 8 and writing a JAVA code to create new folder "ABC" under "C:\Program Files" with Read and Write privileges for all users.

But after execution of program, folder is not created in specified path.

I am using following code to create a folder.

public class PermissionTest {
    public static void main(String[] args) {
       String path = "C:\\Program Files\\ABC";
       //String path = "D:\\ABC";
       File file = new File(path);
       file.mkdirs();
       file.setReadable(true, false);
       System.out.println("ABC readable  "+file.canRead());
       file.setWritable(true, false);
       System.out.println("ABC writable  "+file.canWrite());
       file.setExecutable(true, false);
       System.out.println("ABC executable  "+file.canWrite());
    }        
}

Output:

ABC readable  false
ABC writable  false
ABC executable  false

Is there any other approach to create a folder and assign Read and Write permission to all user under "C:\Program Files"?

Please Advise.

Mayur Bhokase
  • 377
  • 4
  • 19
  • Run the program as an administrator. If command line then run your cmd as admin. This will fix the issue. – Ria Sen Apr 21 '17 at 06:47
  • Is there any other way to solve this issue in programmatic way to avoid manual operation? – Mayur Bhokase Apr 21 '17 at 06:51
  • see e.g. http://stackoverflow.com/questions/1385866/java-run-as-administrator – home Apr 21 '17 at 06:54
  • There is way to give read, write rights to all the applications which is not at all recommended because if may led to corrupting your PC. – Ria Sen Apr 21 '17 at 06:55
  • @home gave the link but it is not suggested at all as your PC then may become vulnerable. – Ria Sen Apr 21 '17 at 06:56
  • @Learner: it's not about the accepted answer, there are some further links and with some more research one might find even better information. Whether this makes sense from a security perspective is a different story... of course only those application requiring access to a certain folder should elevate their prviliges – home Apr 21 '17 at 07:01
  • Many thanks Learner and Home, But I want solution in Java to embed working code in my Existing Java Application. Is there in other way in JAVA to overcome this problem? – Mayur Bhokase Apr 21 '17 at 07:03
  • @home being a developer always think on a longer run else, you will fix it now but creat unknown troubles for future. – Ria Sen Apr 21 '17 at 07:04
  • @Learner: not sure what you're trying to tell me – home Apr 21 '17 at 07:06
  • https://technet.microsoft.com/en-us/library/ff431742.aspx Use this link to make your application always run as administrator. Hope this helps. Please comment if it works then I will add the answer. – Ria Sen Apr 21 '17 at 07:08
  • @RiaSen - As I have already mentioned in my previous comments, we have handle this problem in JAVA code only, so that we apply same solution to our existing JAVA Application. If I used "D:\\ABC" path then it is working properly but its is not working in path "C:\Program Files". – Mayur Bhokase Apr 21 '17 at 08:52

0 Answers0