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

扶醉桌前 提交于 2019-12-02 05:58:36

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"; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!