javamultithreadingappletaccesscontrolexception

java.security.AccessControlException: Occured when Running an applet


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)

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?

Here is My Applet Code.

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);
}
}

Solution

  • 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.

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