javaappletself-signedsecurityexceptionappletviewer

Self-signed Applet throws SecurityException when writing to file


I want to make an Applet that's capable of downloading files to a computer, and then opening them in the associated editor (when the file is saved, it is supposed to be uploaded back again). However, before I spend hours getting it to work, I have to make sure that it is actually manageable (Have done it with a Java Desktop Application just not an Applet).

So I wrote a simple applet that creates a file if it doesn't exist. The app is signed and loads in the browser as it should. The following is written to the screen:

IO Exception: Access is denied

I have labeled the different errors, so I know which one that fails. Below is my applet:

import javax.swing.*;
import java.security.*;
import java.io.*;

public class DocumentApplet extends JApplet
{
    private static final long serialVersionUID = -2354727776089972258L;

    public void start ()
    {
        add ( new JButton ("Hello, World") );

        AccessControlContext acc = (AccessControlContext) System.getSecurityManager().getSecurityContext();
        try
        {
            acc.checkPermission(new FilePermission("test.txt", "write"));
        }
        catch (SecurityException e)
        {
            add (new JLabel ("Permission Exception: " + e.getMessage()));
            return;
        }

        try
        {
            File f = AccessController.<File>doPrivileged(new PrivilegedAction<File>()
            {
                public File run()
                {
                    return new File ("test.txt");
                }
            });

            if ( ! f.exists())
            {
                f.createNewFile();
            }
        }
        catch (AccessControlException e)
        {
            add (new JLabel ("Access: " + e.getMessage()));
        }
        catch (IOException e)
        {
            add ( new JLabel ("IO Exception: " + e.getMessage()));
        }
    }
}

It is the last exception that is being thrown. Note that the first thing I do, is to check permissions. That check does not fail.

The Applet is self-signed, but this is only temporary. I do not want to spend hundreds of dollars in buying a certificate, if the applet is failing...

When I run the app with appletviewer, the code works. That's OK, but I need to know that it will work when I buy a real certificate.

HTML Code:

<applet code="DocumentApplet" archive="applet.jar" width="300" height="200">
</applet>

Environment: Windows7 + JDK 1.7.0_05

PS: I have also spent the last two days reading on Stackoverflow and searching Google. I strongly believe I have done everything I am supposed to do...


Solution

  • I have no idea what the reason for this is, but I managed to write to the file successfully by prefixing the filename with:

    System.getProperty("java.io.tmpdir")
    

    or

    System.getProperty("user.home")
    

    It was a wild shot, because since the error message told me "Access denied", I thought it was because of the security stuff.

    So to all other who may have the same problem: