0

I have embedded an applet in an html page which access User's file directory. Its signed. When I write code inside init function then it works very well but when I write this code inside a method and call it with Javascript Then it sends me security exception. Have u any idea how can I solve this problem?

public class App extends javax.swing.JApplet {

@Override
public void init() {


 }

public void callMethod(){
    File file = new File("D:/test.txt");
    if(!file.exists()){
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 }

}


Javascript:  
    window.document.applets[0].callMethod();
user704006
  • 271
  • 1
  • 5
  • 14

2 Answers2

2

See if the answers to this very similar question can help you: signed applet gives AccessControlException: access denied, when calling from javascript

Community
  • 1
  • 1
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • @WhiteFand34 the problem is same as you've provided link but I couldn't understand solution. He is reloading applet at onClick Event which I don't want to do because it takes a lot of time to load all requried jars etc. What I am trying to achieve is, load applet when page loads and call method when needed. If I will reload all applet again then it stucks browser. – user704006 Apr 30 '11 at 22:13
2

To be trusted, every frame on the stack must be accounted for. Once JavaScript is in the mix, that stops being the case.

To fix it, wrap the trusted code in an AccessController.doPrivileged() method. See the JavaDocs for an example.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433