I keep getting this error message whenever I try to run the applet inside a web browser.
However if I run the client as a swing application like this, it runs fine.
This is my html code.
The jar file contains the ChatClient and the ChatInterface classes and was created by netbeans, all the files are running from the same directory.
Code:
java.sexurity.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
Code:
import java.rmi.*;
public class ChatServer {
public ChatServer() {
try {
ChatImplementation ci = new ChatImplementation();
Naming.rebind("rmi://localhost/ChatService", ci);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
new ChatServer();
}
}
Code:
import java.rmi.*;
import javax.swing.*;
import java.util.*;
public interface ChatInterface extends Remote {
public void logOn(String name) throws RemoteException;
public void sendText(String text) throws RemoteException;
public JTextArea updateChat() throws RemoteException;
public Vector getUsers() throws RemoteException;
}
Code:
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
import javax.swing.*;
public class ChatImplementation extends UnicastRemoteObject implements ChatInterface {
private Vector userList = new Vector();
private JTextArea chatText = new JTextArea();
public ChatImplementation() throws RemoteException {
super();
}
public void logOn(String name) throws RemoteException {
userList.add(name);
}
public void sendText(String text) throws RemoteException {
chatText.append(text + "\n");
}
public JTextArea updateChat() throws RemoteException {
return chatText;
}
public Vector getUsers() throws RemoteException {
return userList;
}
}
Code:
package program;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
public class ChatClient extends JApplet implements ActionListener {
JTextArea userText;
JTextArea chatText;
JButton send;
JList userList;
ChatInterface c;
String nick;
public void init() {
try {
c = (ChatInterface) Naming.lookup("rmi://localhost/ChatService");
JPanel contentPanel = new JPanel();
setContentPane(contentPanel);
contentPanel.setLayout(new BorderLayout());
JPanel textEntry = new JPanel();
userList = new JList(c.getUsers());
JScrollPane usersList = new JScrollPane(userList);
chatText = c.updateChat();
JScrollPane chatScrollPane = new JScrollPane(chatText);
contentPanel.add(chatScrollPane, BorderLayout.CENTER);
contentPanel.add(textEntry, BorderLayout.SOUTH);
contentPanel.add(usersList, BorderLayout.EAST);
userText = new JTextArea (3, 30);
JScrollPane userScrollPane = new JScrollPane(userText);
textEntry.add(userScrollPane);
send = new JButton("Send");
send.addActionListener(this);
textEntry.add(send);
userList.setPrototypeCellValue("------------------------");
chatText.setLineWrap(true);
userText.setLineWrap(true);
chatText.setEditable(false);
c.logOn(JOptionPane.showInputDialog(null, "Please enter a user name"));
updateUsers();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void updateChat() {
try {
chatText.setText(c.updateChat().getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void updateUsers() {
try {
userList.setListData(c.getUsers());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void actionPerformed(ActionEvent ae) {
if (! userText.getText().equals("")) {
try {
c.sendText(nick + ": " + userText.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
userText.setText(null);
updateChat();
}
}
}
However if I run the client as a swing application like this, it runs fine.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
public class ChatClient extends JFrame implements ActionListener {
JTextArea userText;
JTextArea chatText;
JButton send;
JList userList;
ChatInterface c;
String nick;
public ChatClient() {
super();
try {
c = (ChatInterface) Naming.lookup("rmi://localhost/ChatService");
JPanel contentPanel = new JPanel();
setContentPane(contentPanel);
contentPanel.setLayout(new BorderLayout());
JPanel textEntry = new JPanel();
userList = new JList(c.getUsers());
JScrollPane usersList = new JScrollPane(userList);
chatText = c.updateChat();
JScrollPane chatScrollPane = new JScrollPane(chatText);
contentPanel.add(chatScrollPane, BorderLayout.CENTER);
contentPanel.add(textEntry, BorderLayout.SOUTH);
contentPanel.add(usersList, BorderLayout.EAST);
userText = new JTextArea (3, 30);
JScrollPane userScrollPane = new JScrollPane(userText);
textEntry.add(userScrollPane);
send = new JButton("Send");
send.addActionListener(this);
textEntry.add(send);
userList.setPrototypeCellValue("------------------------");
chatText.setLineWrap(true);
userText.setLineWrap(true);
chatText.setEditable(false);
} catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}
nick = JOptionPane.showInputDialog(null, "Please enter a user name");
logOn(nick);
setVisible(true);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void logOn(String name) {
try {
c.logOn(name);
updateUsers();
} catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}
}
public void updateChat() {
try {
chatText.setText(c.updateChat().getText());
} catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}
}
public void updateUsers() {
try {
userList.setListData(c.getUsers());
} catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}
}
public void actionPerformed(ActionEvent ae) {
if (! userText.getText().equals("")) {
try {
c.sendText(nick + ": " + userText.getText());
} catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}
userText.setText(null);
updateChat();
}
}
public static void main(String[] args) {
new ChatClient();
}
}
This is my html code.
Code:
<html>
<applet code = 'program.ChatClient'
archive = 'Program.jar',
width = 600,
height = 600 />
</html>
The jar file contains the ChatClient and the ChatInterface classes and was created by netbeans, all the files are running from the same directory.
Last edited: