1

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);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    In the 3rd millennium, we would do this: 1) Using Swing. 2) Animating using a `javax.swing.Timer` 3) ..and if it is in a web page, ignore all that and use the HTML 5 canvas for rendering and JavaScript for controlling the animation. – Andrew Thompson Sep 01 '12 at 11:27
  • 2
    *"Can anyone explain in **detail** why it is Throwing Security Exception and its Solution in **simple** terms?"* If we explain in detail it cannot be simple. Choose one or the other. – Andrew Thompson Sep 01 '12 at 11:32
  • As an aside. It is worrying that book claims to cover 'Java 2' in which Swing (e.g. `JApplet`) was introduced, while seemingly leading to a code sampled rooted firmly in AWT (`Applet`). – Andrew Thompson Sep 01 '12 at 11:37
  • You do not need a `ThreadGroup` to run this example. Try removing the use of that class. – Sreenath S Sep 01 '12 at 12:42

1 Answers1

1

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";
  ...
};
Drona
  • 6,886
  • 1
  • 29
  • 35