java.security.AccessControlException:运行小程序时发生 [英] java.security.AccessControlException: Occured when Running an applet

查看:144
本文介绍了java.security.AccessControlException:运行小程序时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Herbert Schildt的书Java2 Complete Reference Fifth Edition中逐步学习Java.在创建简单的Banner小程序的过程中,该方法通过创建Thread并调用Applet的Repaint()方法来在Applet Viewer上显示并显示Banner.但是在创建可运行Target的线程对象时,它会抛出这样的异常

I am Learning Java Step by Step From Herbert Schildt Book Java2 Complete Reference Fifth Edition. On my way to creating simple Banner Applet which Display's Banner and Scrolls it on Applet Viewer by creating Thread and calling Repaint() method of Applet. but while crating thread object of runnable Target it throws exception like this

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文件中已经允许了所有权限

I read Other Article saying it need Security Permission but in My applet.policy file there is already all permission allowed

grant { permission java.security.AllPermission;};

这只是我的第二个小程序.谁能简单地详细解释为什么它是引发安全性异常"及其解决方案?

This is Only my Second Applet. Can anyone explain in detail why it is Throwing Security Exception and its Solution in simple terms?

这是我的小程序代码.

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安全目录下的全局策略文件.

Applets, by default, run in a sandbox environment with restricted permissions because of security reasons. Applets do not have runtime permissions to create or modify thread groups and hence you are getting the exception. Do not create a new thread group. Or else override your security policy to explicitly allow your applet to create one by granting the runtime permission to create or modify thread group. To override default permissions define appropriate policy in your user home's .java.policy file. It is recommended that you edit your user specific policy file and not the global policy file under your JRE security directory.

使用JDK的策略工具定义策略或手动执行.请参考下面的模板:

Use a JDK's policy tool to define the policy or do it manually. Refer the template below:

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";
  ...
};

这篇关于java.security.AccessControlException:运行小程序时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆