java.rmi.Naming Class in Java
Last Updated :
20 Jul, 2022
Java.rmi.Naming class contains a method to bind, unbind or rebind names with a remote object present at the remote registry. This class is also used to get the reference of the object present at remote registries or the list of name associated with this registry.
Syntax: Class declaration
public final class Naming extends Object
Let us do discuss some major methods of this class which are as follows:
The most commonly used methods are described below as follows:
- bind()
- list()
- lookup()
- rebind()
- unbind()
Method 1: bind()
Syntax:
static void bind(String name, remote object)
// Bind this name with this remote object
Parameters: Name of the remote registry
Exceptions:
- AlreadyBoundException occurs when the name was already bounded
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
Method 2: list() Used to get the list of the names associated with the registry
Syntax:
static string[] list(String name)
Parameters: The name of the remote registry
Return Type: An array of name bounded with this registry
Exceptions:
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when hen the contact to remote registry fails
Method 3: lookup() looks for the reference of the remote object to which this name is associated
Syntax:
static remote lookup(String name)
Parameters: The name of the remote registry
Return Type: The reference for a remote object
Exceptions:
- NotBoundException occurs when the name does not bound with the current operation
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
- MalformedURLException occurs when the format of the name is not an URL
Method 4: rebind() method rebinds this name with the associated remote object
Syntax:
static void rebind(String name, remote object)
Parameters: It takes two parameters namely name and object.
- The name of the remote registry
- The remote object to associate with the name
Exceptions:
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
Method 5: unbind() unbinds this name with the associated remote object
Syntax:
static void unbind(String name)
Parameters: The name of the remote registry
Exceptions:
- NotBoundException occurs when the name does not bound with the current operation
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
Example 1:
Java
// Java Program to create a registry
// Server's Side
// Importing java.rmi package to enable object of one JVM
// to invoke methods on object in another JVM
import java.rmi.*;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
// Interface
// creating remote interface
interface demo extends Remote {
public String msg(String msg) throws RemoteException;
}
// Class 1
// Helper Class
class demoRemote
extends UnicastRemoteObject implements demo {
demoRemote() throws RemoteException { super(); }
// @Override
public String msg(String msg) throws RemoteException
{
// Display message only
System.out.println("GeeksForGeeks");
return null;
}
}
// Class 2
// Main class
public class GFG_Server {
// Main driver method
public static void main(String args[])
throws RemoteException, NotBoundException,
AlreadyBoundException
{
// Try block to check for exceptions
try {
// Creating a new registry by creating
// an object of Registry class
Registry registry
= LocateRegistry.createRegistry(3000);
demo obj = new demoRemote();
// binding obj to remote registry
Naming.bind("rmi://localhost:3000"
+ "/geeksforgeeks",
(Remote)obj);
// Display message when program
// is executed successfully
System.out.println(
"registry created successfully");
}
// Catch block to handle the exceptions
catch (Exception e) {
// Getting the name of the exception using
// the toString() method over exception object
System.err.println(e.toString());
}
}
}
Output:
registry created successfully
Implementation: Java program to look for the object(Clients' side)
Example 2:
Java
// Java Program to look for the object
// Client Side
// Importing java.rmi package to enable object of one JVM
// to invoke methods on object in another JVM
import java.rmi.*;
// Main class
public class GFG_Client {
// Main driver method
public static void main(String args[])
{
// try block to check for exceptions
try {
// Looking for object in remote registry
demo client = (demo)Naming.lookup(
"rmi://localhost:3000/obj");
// Print and display the client message
System.out.println(client.msg());
}
// Catch block to handle the exception
catch (Exception e) {
}
}
}
Output:
GeeksForGeeks
Similar Reads
Java.lang.Runtime class in Java In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java
6 min read
Java.lang.Package Class in Java In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loade
9 min read
java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
4 min read
java.lang.reflect.Field Class in Java The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
5 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
java.lang.reflect.Method Class in Java java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to oc
3 min read
java.lang.reflect.Modifier Class in Java The java.lang.reflect.Modifier class contains methods used to get information about class, member and method access modifiers. The modifiers are represented as int value with set bits at distinct positions. The int value represents different modifiers. These values are taken from the tables in secti
7 min read
java.rmi.RMISecurityManager Class in Java The RMISecurityManager enforces the security policy for classes that are loaded as stubs for remote objects, by overriding all of the relevant access-check methods from the SecurityManager. By default, stub objects are only allowed to perform class definition and class access operations. Note: If
3 min read
java.rmi.MarshalledObject Class in Java java.rmi.MarshalledObject is a java class, a MarshalledObject contains a byte stream with the serialized representation of an object given to its constructor, The contained object is serialized and deserialized with the same serialization semantics used for marshaling and unmarshaling parameters. Si
4 min read
java.lang.reflect.Parameter Class in Java java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for
4 min read