1

I am trying to test out RMI of java, but i cannot get the rmic command to work.

Screenshot

Do tell if you need my code, although i am pretty sure it does not matter, i have implemented the RMI functions correctly.

Server Side Code:

  public static void main(String[] args)
    {
        try {
            Registry r = LocateRegistry.getRegistry();
            r.bind("RService", new RSimpl());
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot Start Remote Service");
        }
        System.out.println("Remote Service Is Running");
    }   

Client Side Code:

@SuppressWarnings("deprecation")
    public Object[] getServiceList()
    {
        boolean connectionsuccess = false;
        Object[] objList = null;
        try {
            System.setSecurityManager(new RMISecurityManager());
            server = (RemoteService) Naming.lookup("rmi://192.168.1.77/RService");
            connectionsuccess = true;
        }
        catch(Exception ex) {
            Object[] options = {"Retry","Cancel"};
            int o = JOptionPane.showOptionDialog(frame, "Cannot Establish Connection To The Server. \n Do You Want To Retry?", "Connection Error", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if(o==0)
            {
                i=0;
                attemptConnect();
            }
            else
            {
                i=1;
                return error;
            }
        }
    }

RSimpl.java:

    package testrmi;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
import java.util.*;

public class RSimpl extends UnicastRemoteObject implements RemoteService {

    HashMap map;

    public RSimpl() throws RemoteException
    {
        setUpServices();
    }

    public void setUpServices()
    {
        map = new HashMap();
        map.put("Dice Roll Service", new DiceRoll());
        map.put("Calculator Service", new Calculator());
        // Keep Adding Services On The Go!
    }

    public Object[] getServiceList()
    {
        return map.keySet().toArray();
    }

    public Service getService(Object SvcKey)
    {
        Service theService = (Service) map.get(SvcKey);
        return theService;
    }

    public static void main(String[] args)
    {
        try {
            Registry r = LocateRegistry.getRegistry();
            r.bind("RService", new RSimpl());
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot Start Remote Service");
        }
        System.out.println("Remote Service Is Running");
    }   
}

RemoteService.java (interface):

package testrmi;

import java.rmi.*;

public interface RemoteService extends Remote {

    public Object[] getServiceList() throws RemoteException;
    public Service getService(Object SvcKey) throws RemoteException;

}
Hanzyusuf
  • 369
  • 5
  • 11

3 Answers3

2

The class file for a class named testrmi.RSImpl must be in testrmi/RSImpl.class relative to one of the directories named in the CLASSPATH.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Seems like the file can't be found in this case. Try typing dir in the cmd to see if your folder is there. You can also navigate into the testrmi folder and type rmic RSimpl.

If it still doesn't work try copying the files you need into a folder on desktop, navigate there and test.

Lone
  • 9
  • 2
  • Can I take a look at your remote interface too? RSimpl.java – Lone Jul 17 '17 at 11:54
  • i have added the classes in my question, please take a look, sorry for the late reply – Hanzyusuf Jul 17 '17 at 12:11
  • Create a folder in desktop lets name it testrmi. Next put the files you need into that folder. Compile everything and using cmd navigate to desktop. Type rmic testrmi.RSimpl. Start rmi registry. Everything should work fine. – Lone Jul 17 '17 at 12:42
  • Update: Still not working, please help i need to start a new book, but this has been frying up my mind since morning :( – Hanzyusuf Jul 17 '17 at 13:02
  • I don't think that main method is needed in RSimpl. Add @SuppressWarnings("deprecation") to RSimpl too. – Lone Jul 17 '17 at 13:10
-1

Sorry for my stupid question... I did not add classpath as defined in RMIC documentation on oracle. For anyone else facing this problem, open your System Environment Variables and Add those two path variables:

  • .;
  • C:\user\myproject\myCustomCompiledClasses

you MUST add the DOT path variable and also the path to your compiled classes

Hanzyusuf
  • 369
  • 5
  • 11