Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 06:38 PM
DylanDubya DylanDubya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 3 DylanDubya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 52 sec
Reputation Power: 0
Homework - 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.

Reply With Quote
  #2  
Old December 11th, 2012, 07:59 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #3  
Old December 11th, 2012, 12:32 PM
DylanDubya DylanDubya is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 3 DylanDubya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 52 sec
Reputation 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

Reply With Quote
  #4  
Old December 11th, 2012, 12:38 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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:
Quote:

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Java.security.AccessControlException? Urgent!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap