Getting started and using Java RMI

Keywords: Java rpc

Java RMI


When different JVM needs to be called each other, how to make the method of calling JVMB in JVMA as local, this is the RMI-Remote Method Invoke.

Different JVMs can refer to different JVMs on the same machine or value JVMs on different machines

Basic principles

As for stubs and skeletons, I think they are difficult to remember, and these two words are not easy to be implied, so I'll talk about them with the help of the current popular concept of microservice.

If you know something about micro services, the invocation process of RMI is actually very similar to it.

  • A registry - RMI Registry is required. This contains the name of the remote service and the specific implementation class
  • The server needs to provide an interface that can make remote calls
  • The server also needs to implement the above interfaces
  • The client finds the name of the required service in RMI Registry, obtains its instance, and makes remote calls

If you really want to understand various details, it is highly recommended to read the agent mode chapter in the book Head First design mode, which vividly introduces the basic principles and details of RMI


The server provides an interface for remote call

  1. Inherit Remote interface
  2. The method providing remote service needs to throw RemoteException
public interface RmiServerInterface extends Remote {
     * All methods that provide RMI must throw RemoteException, otherwise an error will be reported
     * Exception "remote object implements illegal remote interface"?
     * error
     * @return
     * @throws RemoteException
    String sayHello() throws RemoteException;

The server implements the remote interface

  1. Inherit UnicastRemoteObject
  2. The parameterless constructor throws a RemoteException
public class RmiInterfaceImpl extends UnicastRemoteObject implements RmiServerInterface{
    public RmiInterfaceImpl() throws RemoteException{
    public String sayHello() {
        return "hello, I'm Remote JVM Server";

Registration Center

public class Server {
    public static void main(String[] args) throws RemoteException {
        // Start registry
        Registry registry = LocateRegistry.createRegistry(2001);
        try {
          // Binding service
            registry.bind("hello", new RmiInterfaceImpl());
            System.err.println("Server ready");
        } catch (AlreadyBoundException e) {
            System.out.println("Server bind fail" + e.getMessage());

Remote call from client

  1. Find directly in RMI Registry
public class RmiClient {
    public static void main(String[] args) {
        try {
          // The search name must be a name registered in the registry
            RmiServerInterface hello = (RmiServerInterface) Naming.lookup("rmi://localhost:2001/hello");
            System.out.println("The returned information received from the server interface is: " + hello.sayHello() +" "+ hello.getClass().getName());
        } catch (RemoteException | NotBoundException | MalformedURLException e) {
            System.out.println("Connect Server fail"+ e.getMessage());


  1. Start RMI Registry
  2. Start the client and observe the print information. You can find that the class obtained by the client is actually a proxy class


  1. If the server returns a class, the class must implement the serialization interface because of the design of network transmission and IO. Therefore, the serialization interface must be implemented
  2. Code warehouse
  3. RPC can be thought of from RMI. What is the relationship between the two? Personally, RMI is a subset of RPC. RMI is the customized development of RPC for Java language and object characteristics, which makes it very convenient for Java developers to call between different JVM s

Official documents

Note: when coding according to official documents, there is always a problem in setting codebase

Posted by christo16 on Fri, 01 Oct 2021 17:38:46 -0700