Search code examples
javarmi

Accessing RMI Server


I would like to access an existing RMI server. I do not have access to its source code, so I would like to set up a client that calls an existing service in the RMI Server.

What is the best way to create a client to invoke a particular method and can RMI be converted to a webservice?

Below is the class I have created:

package server;

import com.SerialService;
import java.rmi.Naming;

public class NewClass {

    public static void main(String args[]) {
        //SerialService obj = null;
        try {

            SerialService obj = (SerialService) Naming.lookup("//localhost:8083/");
            System.out.println(obj.getSerial());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Solution

  • You will need to get the RMI services client stub so you can call/compile against. You would still need to know the port that the service is listening on.

    import java.rmi.Naming;
    
    public class RmiClient { 
      public static void main(String args[]) throws Exception {
        RmiServerIntf obj = (RmiServerIntf)Naming.lookup("//localhost/RmiServer");
        System.out.println(obj.getMessage()); 
      }
    }