0
votes

I'm developping a Java Applet that must access the visitor's filesystem, so i compressed my .class file to a .jar file with self-signed cert, when I'm opening the call.html file with my browser (file where is located the <applet> HTML tag), I accept the security popup then i'm getting this error in the Java console:

java.security.AccessControlException: access denied (java.io.FilePermission output.txt write)

I'm using a FileInputStream and a FileOutputStream. FileInputStream works but not FileOutputStream, why?

Here's my code:

try {
      AccessController.doPrivileged(
          new PrivilegedExceptionAction() {              
              @Override 
              public Object run() throws FileNotFoundException {
                  outFile = new FileOutputStream("output.txt");
                  inFile = new FileInputStream("input.txt");
                  return "test";
              }
          }
      );
} catch (PrivilegedActionException e) {
    throw (FileNotFoundException) e.getException();
}

I've tried many way to make privileged actions, FileInputStream is always working, whereas FileOutputStream isn't. output.txt is not read-only file.

1
Could it be that you are writing to a read only directory? Check the full path and the file system permissions. - jontro
I would suggest writing the file to a sub-directory of user.home. It is a place that should be allowable for a trusted app. to write. What is in the output? (E.G. Game scores, the customer's billing record..) - Andrew Thompson
The directory isn't in read only mode. How to access user.home ? Netbeans show me this error: "Cannot find symbol: user". - user1376701
Update: by using outFile = new FileOutputStream(System.getProperty("user.home")+"/output.txt"); i'm getting this error: java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read) - user1376701
@andrew-thompson I want to put some datas in the output.txt file temporary, is there a temp storage under the Java sandbox where i can create my output.txt file? I tried `new FileOutputStream("output.txt") but -> error. - user1376701

1 Answers

1
votes

Access permission is granted with a policy file, and appletviewer is launched with the policy file to be used for the applet being viewed.

Creating a Policy File

Policy tool is a Java 2 Platform security tool for creating policy files. The Java Tutorial trail on Controlling Applets explains how to use Policy Tool in good detail. Here is the policy file you need to run the applet. You can use Policy tool to create it or copy the text below into an ASCII file.

grant {
       permission java.util.PropertyPermission 
   "user.home", "read";
       permission java.io.FilePermission 
   "${user.home}/text.txt", "read,write";
     };

Here is the full link for applets permission http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/data.html