程序代码如下:import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;/**
   This class demonstrates the use of a custom security manager
   that prohibits the reading of text files containing bad words.
*/
public class SecurityManagerTest
{  
   public static void main(String[] args)
   {  
      System.setSecurityManager(new WordCheckSecurityManager());
      JFrame frame = new SecurityManagerFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}/**
   This frame contains a text field to enter a file name and
   a text area to show the contents of the loaded file.
*/
class SecurityManagerFrame extends JFrame
{  
   public SecurityManagerFrame()
   {  
      setTitle("SecurityManagerTest");
      setSize(WIDTH, HEIGHT);      fileNameField = new JTextField(20);
      JPanel panel = new JPanel();
      panel.add(new JLabel("Text file:"));
      panel.add(fileNameField);
      JButton openButton = new JButton("Open");
      panel.add(openButton);
      openButton.addActionListener(new
         ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               loadFile(fileNameField.getText());
            }
         });      Container contentPane = getContentPane();
      contentPane.add(panel, "North");      fileText = new JTextArea();
      contentPane.add(new JScrollPane(fileText), "Center");
   }   /**
      Attempt to load a file into the text area. If a security
      exception is caught, a message is inserted into the text
      area instead.
      @param filename the file name
   */
   public void loadFile(String filename)
   {  
      try
      {  
         fileText.setText("");
         BufferedReader in
            = new BufferedReader(new FileReader(filename));
         String s;
         while ((s = in.readLine()) != null)
         fileText.append(s + "\n");
         in.close();
      }
      catch (IOException e)
      {  
         fileText.append(e + "\n");
      }
      catch (SecurityException e)
      {  
         fileText.append("I am sorry, but I cannot do that.");
      }
   }   private JTextField fileNameField;
   private JTextArea fileText;
   private static final int WIDTH = 400;
   private static final int HEIGHT = 300;
}

解决方案 »

  1.   

    import java.io.*;
    import java.security.*;/**
       This security manager checks whether bad words are
       encountered when reading a file.
    */
    public class WordCheckSecurityManager extends SecurityManager
    {  
       public void checkPermission(Permission p)
       {  
          if (p instanceof FilePermission
             && p.getActions().equals("read"))
          {  
             if (inSameManager())
                return;
             String fileName = p.getName();
             if (containsBadWords(fileName))
                throw new SecurityException("Bad words in "
                   + fileName);
          }
          else super.checkPermission(p);
       }   /**
          Returns true if this manager is called while there
          is another call to itself pending.
          @return true if there are multiple calls to this manager
       */
       public boolean inSameManager()
       {  
          Class[] cc = getClassContext();      // skip past current set of calls to this manager
          int i = 0;
          while (i < cc.length && cc[0] == cc[i])
             i++;      // check if there is another call to this manager
          while (i < cc.length)
          {  
             if (cc[0] == cc[i]) return true;
             i++;
          }
          return false;
       }   /**
          Checks if a file contains bad words.
          @param fileName the name of the file
          @return true if the file name ends with .txt and it 
          contains at least one bad word.
       */
       boolean containsBadWords(String fileName)
       {  
          if (!fileName.toLowerCase().endsWith(".txt")) return false;
             // only check text files
          BufferedReader in = null;
          try
          {  
             in = new BufferedReader(new FileReader(fileName));
             String s;
             while ((s = in.readLine()) != null)
             {  
                for (int i = 0; i < badWords.length; i++)
                if (s.toLowerCase().indexOf(badWords[i]) != -1)
                   return true;
             }
             in.close();
             return false;
          }
          catch(IOException e)
          {  
             return true;
          }
          finally
          {  
             if (in != null)
                try { in.close(); } catch (IOException e) {}
          }
       }   private String[] badWords = { "sex", "drugs", "c++" };
    }
      

  2.   

    应该是在运行时, 而不是在编译时吧.编译时是不会throw exception的
      

  3.   

    是你的CLASSPATH里面没有把当前目录加上
    如果在dos窗口运行你的测试程序,可以试试
    在你的编译输出目录下运行
    set classpath = .;%classpath%
    java SecurityManagerTest
      

  4.   

    楼上说的对
    运行时没配置CLASSPATH
    可能是~~~
      

  5.   

    en  set classpath=.;%classpath%