Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Running Java RMI Application
2.1.
Setting Up The Example Project
2.1.1.
Program
2.1.2.
Program
2.1.3.
Program
2.1.4.
Program
2.2.
2. Executing The Application
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Running Java RMI Application

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Remote Method Invocation(RMI) allows remote communication between Java programs, and you can use it for a distributed object reference system. An object which issues its interface on other machines is a distributed object, whereas a Remote Object is a distributed object whose state encapsulates. 

When we work with an RMI application in Java, running the application is a process that you need to understand to get the implementation correct adequately. We have already learned about an RMI application's concept, usage, and functionality in our previous article, Remote Method Invocation In Java, so be sure to check that out before going further in this article. Through this article, we will learn how we can run an RMI Application in Java step by step with the help of an example.

Running Java RMI Application

One of the key advantages of working with a Java RMI application is the ease of implementation. You can run such an application by following a few steps in order. Here we will go through the steps one by one with the help of an example.

Setting Up The Example Project

Before running the application, we must create some classes and interfaces for our example. 

Firstly, We will create an interface that describes the methods that remote clients can invoke. The interface should extend the Remote interface, and the method prototype in the interface should throw the RemoteException.

Program

import java.rmi.*;
public interface Adder extends Remote{


//service to perform simple addition of two integers
public int add(int x,int y)throws RemoteException;
}

 

Next, we will implement the remote interface. For that, the class should extend to UnicastRemoteObject class of java.rmi package. We also need to make a default constructor throw the java.rmi.RemoteException from its parent constructor in the class.

Program

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


//implementing the adder service
public class AdderRemote extends UnicastRemoteObject implements Adder{


AdderRemote()throws RemoteException{
super();
}


public int add(int x,int y){return x+y;}


}

 

Finally, we need two classes, Server and Client, for our application. 

We create the Server class for hosting a service. In the Server class, we use the rebind method of the Naming class(java.rmi.Naming.rebind()) to bind the remote object to the new name. The method takes an object reference (service name) and an instances reference as its two arguments. Here we are using 5000 as our port number.

Program

import java.rmi.*;


public class MyServer{


public static void main(String args[]){
try{


Adder stub=new AdderRemote();


//we are binding the remote object by the name sonoo
Naming.rebind("rmi://localhost:5000/sonoo",stub);


}catch(Exception e){System.out.println(e);}
}


}

 

We will also create the Client application program, which will invoke the lookup method of the Naming class(java.rmi.Naming.lookup()) method for RMI URL and return an instance of object type (Factorial Interface) for the stub. All RMI occurs on this object.

Program

import java.rmi.*;


public class MyClient{


public static void main(String args[]){
try{


Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));


}catch(Exception e){System.out.println(e);}
}


}

2. Executing The Application

After saving all of the java files above into a standard directory, you can start to execute the application by following a few simple steps.

Firstly, we will compile all of the java files that we saved in our directory.

Next, we will start the RMI registry with the rmiregistry command with the port number that we are using(5000) on the same terminal.


Now we can run our Server file(MyServer).
Finally, we will run the Client file(MyClient) on a separate terminal and obtain the expected 

output from our application.


Also see, Duck Number in Java and Hashcode Method in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

1. What is an RMI application?

Ans: RMI provides a method for developers to communicate and pass information between the server and the client back and forth. An RMI application is also known as distributed object application. It allows an object placed in one system or Java Virtual Machine(JVM) to access or invoke an object running on another JVM.

 

2. What are Marshaling and Unmarshalling in RMI?

Ans: Whenever a client invokes a method accepting parameters on a remote object, the parameters are bundled into a message before sending over the network. These parameters may be of a primitive type or object. In the case of the primitive type parameter, the parameters are put together, and a header is attached. And the objects are serialized in case the parameters are objects. This process is Marshalling.

The packed parameters are unbundled on the server-side, and then the required method is invoked. This process is Unmarshalling.

 

3. What is an RMI Registry?

Ans: An RMI Registry is an area in the memory maintaining the RMI address information of a Java object server. By default, port 1099 hosts the RMI registry. You can change this port using the mxe.registry.port property or by adding a custom port number with the rmiregistry command on the terminal.

 

4. What are some significant drawbacks of using an RMI application?

Ans: Following are some of the key disadvantages that you might face while using an RMI application in Java:

  • RMI objects are less efficient than Socket objects.
  • You cannot implement the code out of the scope of java.
  • RMI applications require closer monitoring of the security issues.

Key Takeaways

This article extensively discussed implementing a Remote Method Invocation Application in Java with a thorough step-by-step explanation of the process. As you saw in this article, working with an RMI application is easy to learn. The knowledge gained here can be very fruitful for developers working with applications in Java.

We hope that this blog has helped you enhance your knowledge regarding RMI applications in Java, and if you would like to learn more, check out our articles on Basics of JavaGetting Started with Java, and Learn OOPS. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Java Classpath
Next article
Java 17 New Features
Live masterclass