Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File check permission

Tags:

java

java-7

I'm trying to check permission of the given file using the following code snippet.

public static void main(String[] args) {
    try{
        FilePermission fp = new FilePermission("E:/test.txt", "read");
        AccessController.checkPermission(fp);
        System.out.println("Ok to open socket");
    } catch (AccessControlException ace) {
        System.out.println(ace);
    }

So when I run it, it gives me following exception:

java.security.AccessControlException: access denied ("java.io.FilePermission" "E:/test.txt" "read")

All the rights are enable on file but it throws me access denied exception.

like image 837
user3257435 Avatar asked Sep 01 '15 11:09

user3257435


People also ask

How do I check permissions on a file?

Step 2 – Right-click the folder or file and click “Properties” in the context menu. Step 3 – Switch to “Security” tab and click “Advanced”. Step 4 – In the “Permissions” tab, you can see the permissions held by users over a particular file or folder.

Which command is used to check file permissions?

Check Permissions in Command-Line with Ls Command If you prefer using the command line, you can easily find a file's permission settings with the ls command, used to list information about files/directories. You can also add the –l option to the command to see the information in the long list format.

What does Permission 644 and 755 mean for a file?

Some file permission examples: 777 - all can read/write/execute (full access). 755 - owner can read/write/execute, group/others can read/execute. 644 - owner can read/write, group/others can read only.

What does Permission 644 represent?

Permissions of 644 mean that the owner of the file has read and write access, while the group members and other users on the system only have read access. For executable files, the equivalent settings would be 700 and 755 which correspond to 600 and 644 except with execution permission.


1 Answers

Use the following snippet to check if you can read the file:

boolean canRead = new java.io.File("E:/test.txt").canRead();

File.canRead checks if the SecurityManager (if there is any) allows to read the file and if you have read rights in the file system.

Using AccessController.checkPermission(fp) will only not throw an exception if there is a security context which implies the permission. This is not the case when you simply start a Java app as in your example.

like image 178
wero Avatar answered Oct 10 '22 17:10

wero