0

Source code:

package com.web;

import com.web.Operation;
import java.applet.*;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.io.*;

public class AppletExample extends Applet {

public void init() {
    try {
        getAppletContext().showDocument(new URL("file:///C:/Users/Victor/Desktop/test.txt"), "_blank");
    }
    catch (MalformedURLException ex) {
        System.out.println(ex.getMessage());
    }
}

public void paint( Graphics g ) {
    Operation op = new Operation();
    op.response();
    g.drawString("Go File", 0,100);
}
}

When I run the Applet using the Appletviewer application the next error comes on screen:

C:\Users\Victor\Desktop\project2\src>appletviewer display.html
Warning: Can't read AppletViewer properties file: C:\Users\Victor\.hotjava\prope
rties Using defaults.
java.security.AccessControlException: access denied ("java.io.FilePermission" "C
:\Users\Victor\Desktop\test.txt" "write")
        at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
        at java.security.AccessController.checkPermission(AccessController.java:
555)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:203)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at com.web.Operation.response(Operation.java:15)
        at com.web.AppletExample.paint(AppletExample.java:25)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
        at sun.awt.RepaintArea.paint(RepaintArea.java:240)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:347)
        at java.awt.Component.dispatchEventImpl(Component.java:4936)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:117)

This is what the class Operation does:

package com.web;

import com.web.AutomatedTelnetClient;
import java.util.*;
import java.io.*;

public class Operation {

public Operation() {
}

public void response() {
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("C://Users/Victor/Desktop/test.txt"));

        AutomatedTelnetClient telnetClient = new AutomatedTelnetClient();
        telnetClient.connect();

        StringBuffer text = telnetClient.sendCommand("display gps");
        telnetClient.disconnect();

        out.write(text.toString());
        out.close();      
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

It seems to be a permission problem to write in the file from the applet, isn't it? How can I solve it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
vicesbur
  • 335
  • 2
  • 5
  • 13
  • `new FileWriter("C://Users/Victor/Desktop/test.txt")` This will fail on any non-Windows machine, and will fail for every Windows machine that is not being run by ..`Victor`. Instead store the information in memory, as an attribute of the `Operation()` class with a getter method. That gets around all permissions required for `File` access. Where is the code for `AutomatedTelnetClient`? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 29 '13 at 09:45

1 Answers1

0

Since it is a file system of the client you are trying to write to. You need to create a jar with the class files and have to sign it as a trusted application. Follow this post on how to sign, then you that jar to load the applet.

shazin
  • 21,379
  • 3
  • 54
  • 71
  • There is no need to sign the jar. It is enough to grant it the required permission in the policy file. – gcvt Jan 28 '13 at 15:35
  • 1
    @Dejan Policy files are not very useful during development, and entirely impractical for deployment. If an applet needs trust, digitally sign it and stop messing about. – Andrew Thompson Jan 29 '13 at 09:31
  • Be sure to read and follow the most up-to-date version of the documentation about signing and java security. There have been some changes in the last updates, e.g. Java 7 Update 40 released today. – mschenk74 Sep 10 '13 at 20:49