Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf-signed Applet throws SecurityException when writing to file
    text
    copied!<p>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).</p> <p>So I wrote a simple applet that creates a file if it doesn't exist. The app is <strong>signed</strong> and loads in the browser as it should. The following is written to the screen:</p> <blockquote> <p>IO Exception: Access is denied</p> </blockquote> <p>I have labeled the different errors, so I know which one that fails. Below is my applet:</p> <pre><code>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.&lt;File&gt;doPrivileged(new PrivilegedAction&lt;File&gt;() { 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())); } } } </code></pre> <p>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.</p> <p>The Applet is self-signed, but this is only temporary. <strong><em>I do not want to spend hundreds of dollars in buying a certificate, if the applet is failing..</em></strong>.</p> <p>When I run the app with <em>appletviewer</em>, the code works. That's OK, but I need to know that it will work when I buy a real certificate.</p> <p>HTML Code:</p> <pre><code>&lt;applet code="DocumentApplet" archive="applet.jar" width="300" height="200"&gt; &lt;/applet&gt; </code></pre> <p>Environment: Windows7 + JDK 1.7.0_05</p> <p>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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload