How to run Java RMI Application Last Updated : 02 Nov, 2018 Comments Improve Suggest changes Like Article Like Report Prerequisite: RMI RMI (Remote Method Invocation) is used for distributed object references system. A distributed object is an object which publishes its interface on other machines. A Remote Object is a distributed object whose state is encapsulated. Stub and Skeleton are two objects used to communicate with the remote object. Stub: Stub is a gateway for client program which is used to communicate with skeleton object, by establishing a connection between them. Skeleton: Resides on Server program which is used for passing the request from stub to the remote interface. How communication and process takes place in RMI: Steps to Run Java RMI Application in Console Creation of classes and interfaces for the problem statement: The steps involved in this are as follows: Create a Remote Interface which extends java.rmi.Remote: A remote interface determines the object that can be invoked remotely by the client. This interface can be communicated with the client's program. This Interface must extend java.rmi.Remote Interface. Problem Statement: Create an RMI Application for finding the factorial of a number Interface Program import java.math.BigInteger; // Creating an Interface public interface Factorial extends java.rmi.Remote { // Declaring the method public BigInteger fact(int num) throws java.rmi.RemoteException; } Create a class which extends java.rmi.server.UnicastRemoteObject and implements the previous interface. This class will implement the remote interface. Do the required calculation for the problem statement. Implementation of Interface import java.math.BigInteger; // Extends and Implement the class // and interface respectively public class FactorialImpl extends java.rmi.server.UnicastRemoteObject implements Factorial { // Constructor Declaration public FactorialImpl() throws java.rmi.RemoteException { super(); } // Calculation for the problem statement // Implementing the method fact() // to find factorial of a number public BigInteger fact(int num) throws java.rmi.RemoteException { BigInteger factorial = BigInteger.ONE; for (int i = 1; i <= num; ++i) { factorial = factorial .multiply( BigInteger .valueOf(i)); } return factorial; } } Create a Server Class (with localhost and service name) For hosting a service, the server program is created whereby using java.rmi.Naming.rebind() method can be called which takes two arguments i.e., an object reference (service name) and instances reference. Server Program import java.rmi.Naming; public class FactorialServer { // Implement the constructor of the class public FactorialServer() { try { // Create a object reference for the interface Factorial c = new FactorialImpl(); // Bind the localhost with the service Naming.rebind("rmi:// localhost/FactorialService", c); } catch (Exception e) { // If any error occur System.out.println("ERR: " + e); } } public static void main(String[] args) { // Create an object new FactorialServer(); } } Create a Client Class (with localhost and service name) Client program will invokes java.rmi.Naming.lookup() method for RMI URL and returns an instance of object type (Factorial Interface). All RMI is done on this object Client Program import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class FactorialClient { public static void main(String[] args) { try { // Create an remote object with the same name // Cast the lookup result to the interface Factorial c = (Factorial); Naming.lookup("rmi:// localhost/FactorialService"); // Call the method for the results System.out.println(c.fact(30)); } // If any error occur catch (MalformedURLException murle) { System.out.println("\nMalformedURLException: " + murle); } catch (RemoteException re) { System.out.println("\nRemoteException: " + re); } catch (NotBoundException nbe) { System.out.println("\nNotBoundException: " + nbe); } catch (java.lang.ArithmeticException ae) { System.out.println("\nArithmeticException: " + ae); } } } Compilation of all program Use javac to compile all four programs and rmic (RMI Compiler) to create a stub and skeleton class files. Running the system: After the compilation phase, the system is now ready to run. To run the system, open three console screen (move to that path where the program resides). One for the client, one for server and one for the RMI Registry. Start with a registry, use rmiregistry, if there is no error registry will start running and now move to second screen. In the second console run the server program and host the FactorialService. It will start and wait for the client connection and it will load the implementation into memory. In the third console, run the client program. In this way RMI can be run in three console for localhost. RMI uses Network stack and TCP/IP Stack for communication of three different JVM's. Comment More infoAdvertise with us Next Article How to run Java RMI Application bilal-hungund Follow Improve Article Tags : Java Technical Scripter Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Like