2

I have an applet class that's trying to read a file in the client's system. but when accessing this file, i get java.io.filePrmission error

java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\my_download\mytext_11May" "read")

I am able to access the file with JRE 1.6. but when the applet uses jre1.8 the above exception is thrown.

My policy file in the user home location:

grant codeBase "file:${java.home}/lib/ext/*" {
  permission java.security.AllPermission;
};

grant {
  permission java.security.AllPermission;
};

grant codeBase "https://*.mydomain.net-" {
  permission java.security.AllPermission;
};

In 1.6, it works only if AllPermission is given in the default grant block.

Below is the code that I am using to access the file:

File sourceDir = null;
try {
    sourceDir = (File) AccessController
        .doPrivileged(new PrivilegedExceptionAction<File>() {
            public File run() {
                    return new File(filePath);
                }
            });
} catch (PrivilegedActionException e) {
    System.out.println(e.getMessage()+"-----");
    e.printStackTrace();
} catch(Exception e) {
    System.out.println("EEEException: " + e.getMessage());
    e.printStackTrace();
}

The strange part is that the catch block is also not executed. It executes the catch block of the caller method.

The jar is signed with all the necessary domains and permissions is as mentioned below.

Permissions: all-permissions

In the same jar when I try a write operation it works fine even without a policy file using versions 1.6 and 1.8. I get this exception only when I try to read it. In 1.6 I overcome by using java policy but this is not helping in version 1.8.

Thanks in advance for the help!

chiwangc
  • 3,566
  • 16
  • 26
  • 32
user4900143
  • 21
  • 1
  • 2

1 Answers1

0

after a lot of research, found the solution.

the method that was being invoked from the applet had to be surrounded in a AccessController.doPrivileged block.

Ex:

public void methForApplet(){
   AccessController
        .doPrivileged(new PrivilegedExceptionAction<Object>() {
            public Object run() {
                    actualMethToBeExecuted();
                    return null;               
            }
    });
}

This worked in java 1.8 and 1.6 without the policy file.

user4900143
  • 21
  • 1
  • 2