javaappletawtawtrobotaccesscontrolexception

java.security.AccessControlException when using java.awt.Robot class for screen capture in applet


I require to capture a web page screen to store it on client's machine whenever client clicks print screen button. For this I googled and got that by embedding an applet with signature(trusted applet) in my jsp page i can do this. So I am trying with a simple applet for an standalone java class. On success I can try it for jsp after signing the applet. What I tried is:

import java.applet.Applet;  
import java.awt.Graphics;  
import java.util.Date;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
/* <applet code = MyApplet.class width="500" height="500">Java Applet for screen capture</applet>  */



public class MyApplet extends Applet {  

  /* Applet Life cycle Methods */  
  public void start()
  { 
   try{
   // capture the whole screen
   BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
   // Save as JPEG
   File file = new File("D:/screencapture.jpg");
   ImageIO.write(screencapture, "jpg", file);
   System.out.println("screen capture finished : ");
     }//try closing...
     catch(Exception e)
     {
       System.out.println("screen capture error : ");
       e.printStackTrace();
     }//catch closing...  
   }//start closing...

 public void stop()
  {  

  }   
}  

I am getting this:

java.security.AccessControlException: access denied ("java.awt.AWTPermission" "createRobot")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
        at java.security.AccessController.checkPermission(AccessController.java:560)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at java.awt.Robot.checkRobotAllowed(Robot.java:170)
        at java.awt.Robot.init(Robot.java:134)
        at java.awt.Robot.<init>(Robot.java:96)
        at MyApplet.start(MyApplet.java:23)
        at sun.applet.AppletPanel.run(AppletPanel.java:474)
        at java.lang.Thread.run(Thread.java:722)

Any help, Any idea will be appriciated.


Solution

  • I solved this problem. All I did is just pasted these lines in java.policy file(just search this file in your java installation folder and you will get it at 3 places and need to paste this at last in all files)

    permission java.awt.AWTPermission "createRobot"; 
    permission java.awt.AWTPermission "accessClipboard"; 
    permission java.awt.AWTPermission "accessEventQueue"; 
    permission java.awt.AWTPermission "showWindowWithoutWarningBanner"; 
    permission java.awt.AWTPermission "readDisplayPixels", "read"; 
    permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";