Group Chat Example using Java RMI with a Graphical User Interface

The aim of this tutorial is to motivate you for creating a very simple project using Java RMI showing an basic example of a distributed system.

You may need to download Eclipse and Java JDK and get them installed on your machine regardless your operating system ( Windows or Linux ).

There are 4 parts in this tutorial, 1) Server Side + 2) Client Side. 3) Packaging 4) Running the Application. The structures of the projects are shown below:


1) Server Side

1 Open Eclipse and create a new Java Project via : File –> New –> Java Project

2 Set the name of the project as : ChatServer


3 Create a new Interface under this project named : ChatServerInt via. Right Click under the project ChatServer–> New–> Interface


4 Type in the following code the interface : ChatServerInt

import java.rmi.*;
import java.util.*;

public interface ChatServerInt extends Remote{	
	public boolean login (ChatClientInt a)throws RemoteException ;
	public void publish (String s)throws RemoteException ;
	public Vector getConnected() throws RemoteException ;

5 Create a new Interface under this project named : ChatClienInt via. Right Click under the project ChatServer–> New–> Interface. Type the following code for the interface:

import java.rmi.*;

public interface ChatClientInt extends Remote{	
	public void tell (String name)throws RemoteException ;
	public String getName()throws RemoteException ;

6 Create a new class named ChatServer under the project. Right Click under the project ChatServer–> New–> Class


7 Type the name of the class as : ChatServer


8 Type the following code for the class ChatServer

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;

public class ChatServer  extends UnicastRemoteObject implements ChatServerInt{
	private Vector v=new Vector();	
	public ChatServer() throws RemoteException{}
	public boolean login(ChatClientInt a) throws RemoteException{	
		System.out.println(a.getName() + "  got connected....");	
		a.tell("You have Connected successfully.");
		publish(a.getName()+ " has just connected.");
		return true;		
	public void publish(String s) throws RemoteException{
		for(int i=0;i<v.size();i++){
		    	ChatClientInt tmp=(ChatClientInt)v.get(i);
		    }catch(Exception e){
		    	//problem with the client not connected.
		    	//Better to remove it

	public Vector getConnected() throws RemoteException{
		return v;

9 We need to create new class under the project ChatServer named as : StartServer. Type the following code for the class StartServer

import java.rmi.*;
import java.rmi.server.*;
public class StartServer {
	public static void main(String[] args) {
		try {
				//System.setSecurityManager(new RMISecurityManager());
				ChatServerInt b=new ChatServer();	
				Naming.rebind("rmi://", b);
				System.out.println("[System] Chat Server is ready.");
			}catch (Exception e) {
					System.out.println("Chat Server failed: " + e);
Change the IP address to your IP address. Better not to use localhost

10 Run your server but for the sake only of compiling through clicking the Green play button shown below:


11 Now, You need to open your DOS cmd console ( or unix terminal). Navigate to your workspace BIN folder for the project. The location of your project can be known through clicking: Select the project ChatServer, Right Click -> Properties


12 Copy the location shown on the next window for the project:


13 Within the CMD black window, type in the full location of the bin folder. For example :

cd C:\Users\imed\workspace\ChatServer\bin

Make sure you type YOUR location, not mine. Make sure that you are inside the BIN folder

14 Run the rmic to generate the stub for the remote object ChatServer Run the following command:

rmic ChatServer -> Press Enter

That’s it almost for the Server Side !

2) Client Side

1 Create a new Java Project via : File –> New –> Java Project, set its name as : ChatClientProj

2 Create a new Interface ChatClientInt under the project ChatClientProj with exactly the same code as above.

3 Create a new Interface ChatServerInt under the project ChatClientProj with exactly the same code as above.

4 Create a new Class : ChatClient under the project ChatClientProj, copy and paste the following code for this class:

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class ChatClient  extends UnicastRemoteObject implements ChatClientInt{
	private String name;
	private ChatUI ui;	
	public ChatClient (String n) throws RemoteException {
	public void tell(String st) throws RemoteException{
	public String getName() throws RemoteException{
		return name;
	public void setGUI(ChatUI t){ 
		ui=t ; 

5 Create a new Class : ChatUI under the project ChatClientProj, copy and paste the following code for this class:

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;
import java.rmi.Naming;
import java.util.*;

public class ChatUI{
  private ChatClient client;
  private ChatServerInt server;
  public void doConnect(){
	    if (connect.getText().equals("Connect")){
	    	if (name.getText().length()<2){JOptionPane.showMessageDialog(frame, "You need to type a name."); return;}
	    	if (ip.getText().length()<2){JOptionPane.showMessageDialog(frame, "You need to type an IP."); return;}	    	
				client=new ChatClient(name.getText());
	    	}catch(Exception e){e.printStackTrace();JOptionPane.showMessageDialog(frame, "ERROR, we wouldn't connect....");}		  
	    	  	//Better to implement Logout ....
  public void sendText(){
    if (connect.getText().equals("Connect")){
    	JOptionPane.showMessageDialog(frame, "You need to connect first."); return;	
      String st=tf.getText();
      st="["+name.getText()+"] "+st;
      //Remove if you are going to implement for remote invocation
  	  	}catch(Exception e){e.printStackTrace();}

  public void writeMsg(String st){  tx.setText(tx.getText()+"\n"+st);  }

  public void updateUsers(Vector v){
      DefaultListModel listModel = new DefaultListModel();
      if(v!=null) for (int i=0;i<v.size();i++){
    	  try{  String tmp=((ChatClientInt)v.get(i)).getName();
    	  }catch(Exception e){e.printStackTrace();}
  public static void main(String [] args){
	System.out.println("Hello World !");
	ChatUI c=new ChatUI();
  //User Interface code.
  public ChatUI(){
	    frame=new JFrame("Group Chat");
	    JPanel main =new JPanel();
	    JPanel top =new JPanel();
	    JPanel cn =new JPanel();
	    JPanel bottom =new JPanel();
	    ip=new JTextField();
	    tf=new JTextField();
	    name=new JTextField();
	    tx=new JTextArea();
	    connect=new JButton("Connect");
	    JButton bt=new JButton("Send");
	    lst=new JList();        
	    main.setLayout(new BorderLayout(5,5));         
	    top.setLayout(new GridLayout(1,0,5,5));   
	    cn.setLayout(new BorderLayout(5,5));
	    bottom.setLayout(new BorderLayout(5,5));
	    top.add(new JLabel("Your name: "));top.add(name);    
	    top.add(new JLabel("Server Address: "));top.add(ip);
	    cn.add(new JScrollPane(tx), BorderLayout.CENTER);        
	    cn.add(lst, BorderLayout.EAST);    
	    bottom.add(tf, BorderLayout.CENTER);    
	    bottom.add(bt, BorderLayout.EAST);
	    main.add(top, BorderLayout.NORTH);
	    main.add(cn, BorderLayout.CENTER);
	    main.add(bottom, BorderLayout.SOUTH);
	    main.setBorder(new EmptyBorder(10, 10, 10, 10) );
	    connect.addActionListener(new ActionListener(){
	      public void actionPerformed(ActionEvent e){ doConnect();   }  });
	    bt.addActionListener(new ActionListener(){
	      public void actionPerformed(ActionEvent e){ sendText();   }  });
	    tf.addActionListener(new ActionListener(){
	      public void actionPerformed(ActionEvent e){ sendText();   }  });
	  JTextArea tx;
	  JTextField tf,ip, name;
	  JButton connect;
	  JList lst;
	  JFrame frame;

6 Run the ChatUI so that everything gets compiled. You can close it

7 Generate the stub for the ChatClient class using the RMIC inside the bin folder of the ChatClientProj

rmic ChatClient -> Press Enter

That’s for the Client now.

3) Packaging of the Applications

1 Copy the stub file for the server side ChatServer_stub.class to the src folder of the client as shown below:


2 Copy the stub file for the client side ChatClient_stub.class to the src folder of the server as shown below:


3 Right click on the project ChatServer, select Refresh:


4 Do the same for the ChatClientProj project, Right click and select Refresh

5 Right Click on the project ChatServer, select Export


6 Select Java –> Runnable Jar file


7 on the next screen, Select ChatServer – StartServer for the launch Configuration.
Browse for a place to save your exported jar file and click Finish, you can name it as server.jar


8 Do the same for the ChatClientProj and export it as client.jar

4) Running the Server & Client

1 The server can be run from Eclipse, or you can use the Server.jar file to run it from any PC through the command (CMD console):

java -jar Server.jar

2 Give the Client.jar file to other people connected to the network. Because the client is a GUI application, they can double click on it to run through Java Virtual Machine ( not WinRar….)
Or instead, to run it through the CMD console as :

java -jar Client.jar

Here a screenshot of the app.