java.security.AccessControlException(运行applet时发生)

君不见长松卧壑困风霜,时来屹立扶明堂。这篇文章主要讲述java.security.AccessControlException:运行applet时发生相关的知识,希望能为你提供帮助。
我正在从Herbert Schildt Book java2 Complete Reference Fifth Edition中逐步学习Java。在我创建简单的Banner Applet的过程中,它通过创建Thread并调用Applet的Repaint()方法来显示横幅并在Applet Viewer上滚动它。但是在创建可运行Target的线程对象时,会抛出这样的异常

java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:345) at java.security.AccessController.checkPermission(AccessController.java:555) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:252) at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:304) at java.lang.ThreadGroup.< init> (ThreadGroup.java:119) at java.lang.ThreadGroup.< init> (ThreadGroup.java:95) at Applet.SimpleBanner.start(SimpleBanner.java:49) at sun.applet.AppletPanel.run(AppletPanel.java:475) at java.lang.Thread.run(Thread.java:713)

我读了其他文章说它需要安全许可但在我的applet.policy文件中已经允许所有权限
grant { permission java.security.AllPermission; };

这只是我的第二个小程序。任何人都可以用简单的术语详细解释为什么它是Throwing Security Exception及其解决方案?
这是我的小程序代码。
import java.applet.Applet; import java.awt.*; /* A Simple Banner Applet. * This Banner Applet Creates a thread that scrolls the message contained * in msg right to left across banner's window. *//* * < applet code="SimpleBanner" width=300 height=50> * < /applet> */public class SimpleBanner extends Applet implements Runnable{ private static final long serialVersionUID = 1L; String msg = "Hello World"; Thread t = null; ThreadGroup Grp; int state; boolean stopflag; /** * Initialization method that will be called after the applet is loaded into * the browser. */ @Override public void init() { //Set Foreground and background color setBackground(Color.cyan); setForeground(Color.red); }@Override public void start() { //Start Thread Grp = new ThreadGroup("Group"); t = new Thread(Grp, this); stopflag = false; t.start(); } //Entry Point for Thread that Runs The banner @Override public void run() { char ch; //Display Banner for(; ; ) { try { repaint(); Thread.sleep(250); ch=msg.charAt(0); msg =msg.substring(1,msg.length()); msg +=ch; if (stopflag) { break; } } catch (InterruptedException ex) {} } }@Override public void stop() { // Pause The Banner stopflag=true; t=null; }@Override public void paint(Graphics g) { //Display The Banner g.drawString(msg, 50, 30); } }

答案默认情况下,由于安全原因,Applet在具有受限权限的沙箱环境中运行。小程序没有运行时权限来创建或修改线程组,因此您将获得异常。不要创建新的线程组。或者覆盖您的安全策略,通过授予运行时创建或修改线程组的权限,明确允许您的applet创建一个。要覆盖默认权限,请在用户主目录的.java.policy文件中定义相应的策略。建议您编辑特定于用户的策略文件,而不是JRE安全目录下的全局策略文件。
【java.security.AccessControlException(运行applet时发生)】使用JDK的策略工具来定义策略或手动执行。请参阅以下模板:
grant codeBase "< code base> " { permission < type> "< target> ", "< actions> "; permission < type> "< target> ", "< actions> "; ... }; For eg. grant codeBase "http://geosim.cs.vt.edu/geosim/-" { permission java.lang.RuntimePermission "modifyThreadGroup"; ... };


    推荐阅读