#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Java.security.AccessControlException? Urgent!


    When I run the two programs below, I run the Server as an application from the command prompt, then I run the Client as an applet. I always get this exception.

    java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:8000" "connect,resolve")
    at java.security.AccessControlContext.checkPermission (AccessControlContext.java:366)
    at java.security.AccessController.checkPermission(Acc essController.java:555)
    at java.lang.SecurityManager.checkPermission(Security Manager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityMan ager.java:1051)
    at java.net.Socket.connect(Socket.java:574)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at AppletClient.init(AppletClient.java:25)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:722)

    The programs are:
    Client:
    Code:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
     
    public class AppletClient extends JApplet {
      // Label for displaying the visit count
      private JLabel jlblCount = new JLabel();
     
      // Indicate if it runs as application
      private boolean isStandAlone = false;
     
      // Host name or ip
      private String host = "localhost";
     
      /** Initialize the applet */
      public void init() {
        add(jlblCount);
     
        try {
          // Create a socket to connect to the server
          Socket socket;
          if (isStandAlone)
            socket = new Socket(host, 8000);
          else
            socket = new Socket(getCodeBase().getHost(), 8000);
     
          // Create an input stream to receive data from the server
          DataInputStream inputFromServer =
            new DataInputStream(socket.getInputStream());
     
          // Receive the count from the server and display it on label
          int count = inputFromServer.readInt();
          jlblCount.setText("You are visitor number " + count);
     
          // Close the stream
          inputFromServer.close();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
      }
     
      /** Run the applet as an application */
      public static void main(String[] args) {
        // Create a frame
        JFrame frame = new JFrame("Applet Client");
     
        // Create an instance of the applet
        AppletClient applet = new AppletClient();
        applet.isStandAlone = true;
     
        // Get host
        if (args.length == 1) applet.host = args[0];
     
        // Add the applet instance to the frame
        frame.add(applet, java.awt.BorderLayout.CENTER);
     
        // Invoke init() and start()
        applet.init();
        applet.start();
     
        // Display the frame
        frame.pack();
        frame.setVisible(true);
      }
    }
    Server:
    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class Server extends JFrame {
      // Text area for displaying contents
      private JTextArea jta = new JTextArea();
     
      public static void main(String[] args) {
        new Server();
      }
     
      public Server() {
        // Place text area on the frame
        setLayout(new BorderLayout());
        add(new JScrollPane(jta), BorderLayout.CENTER);
     
        setTitle("Server");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true); // It is necessary to show the frame here!
     
        try {
          // Create a server socket
          ServerSocket serverSocket = new ServerSocket(8000);
          jta.append("Server started at " + new Date() + '\n');
     
          // Listen for a connection request
          Socket socket = serverSocket.accept();
     
          // Create data input and output streams
          DataInputStream inputFromClient = new DataInputStream(
            socket.getInputStream());
          DataOutputStream outputToClient = new DataOutputStream(
            socket.getOutputStream());
     
          while (true) {
            // Receive radius from the client
            double radius = inputFromClient.readDouble();
     
            // Compute area
            double area = radius * radius * Math.PI;
     
            // Send area back to the client
            outputToClient.writeDouble(area);
     
            jta.append("Radius received from client: " + radius + '\n');
            jta.append("Area found: " + area + '\n');
          }
        }
        catch(IOException ex) {
          System.err.println(ex);
        }
      }
    }
    We are getting the same issue in a project, this is just a much simpler version of the code.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Your applet needs permission to connect.
    Some ways to give it permission:
    sign the jar file
    add an entry to the .java.policy file giving it permission


    How are you executing the applet? If you load it into your browser from a server at the localhost address, the applet should be able to connect to the site it was loaded from.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0
    can you tell me how to add an entry to the java.policy file? How would I do this, and what would I need to add. I tried editing the java.policy file before and it said access denied when I tried to save it.

    I am just running the applet from the IDE. But I've also tried running it from a browser, and running the server application, but to no avail
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Use the policytool to edit the .java.policy file which should be located in the user.home folder. Notice the filename starts with a .
    The path on my Win7
    C:\Users\Norm\.java.policy

    Here's the section of my .java.policy file without a codebase:

    grant {
    permission java.lang.RuntimePermission "modifyThread";
    permission java.lang.RuntimePermission "loadLibrary.HelloNative";
    permission java.util.PropertyPermission "*", "read";
    permission java.lang.RuntimePermission "modifyThreadGroup";
    permission java.io.FilePermission "E:/Java/Java-1_6_0/docs", "read";
    permission java.io.FilePermission "E:/Java/tutorial", "read";
    permission java.net.SocketPermission "127.0.0.1:3000", "connect, resolve";
    };

IMN logo majestic logo threadwatch logo seochat tools logo