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.
i know i'm digging up a dinosaur a year after problem, but i've been facing the same problem. as someone said, changing policy file is very bad idea (also uncomfortable for some users, and as in my case, totally unacceptable solution).
I've been facing the same problem in signed applet with valid mannifest. the problem was in the way i was calling security related method. in this case you should replace line:
BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
with
BufferedImage screencapture = AccessController.doPrivileged(new PrivilegedAction<BufferedImage >() {
@Override
public BufferedImage run(){
return new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
}
});
that is clean way to do it. in appled signed using valid certyficate, with correct security entry in mannifest and jnpl file it works perfect.
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";
来源:https://stackoverflow.com/questions/15381538/java-security-accesscontrolexception-when-using-java-awt-robot-class-for-screen