- 1AclNotFoundException in Java
- 2ActivationException in Java
- 3AlreadyBoundException in Java
- 4ApplicationException in Java
- 5AWTException in Java
- 6BackingStoreException in Java
- 7BadAttributeValueExpException in Java
- 8BadBinaryOpValueExpException in Java
- 9BadLocationException in Java
- 10BadStringOperationException in Java
- 11BrokenBarrierException in Java
- 12CertificateException in Java
- 13CloneNotSupportedException in Java
- 14DataFormatException in Java
- 15DatatypeConfigurationException in Java
- 16DestroyFailedException in Java
- 17ExecutionException in Java
- 18ExpandVetoException in Java
- 19FontFormatException in Java
- 20GeneralSecurityException in Java
- 21GSSException in Java
- 22IllegalClassFormatException in Java
- 23InterruptedException in Java
- 24IntrospectionException in Java
- 25InvalidApplicationException in Java
- 26InvalidMidiDataException in Java
- 27InvalidPreferencesFormatException in Java
- 28InvalidTargetObjectTypeException in Java
- 29IOException in Java
- 30JAXBException in Java
- 31JMException in Java
- 32KeySelectorException in Java
- 33LambdaConversionException in Java
- 34LastOwnerException in Java
- 35LineUnavailableException in Java
- 36MarshalException in Java
- 37MidiUnavailableException in Java
- 38MimeTypeParseException in Java
- 39NamingException in Java
- 40NoninvertibleTransformException in Java
- 41NotBoundException in Java
- 42NotOwnerException in Java
- 43ParseException in Java
- 44ParserConfigurationException in Java
- 45PrinterException in Java
- 46PrintException in Java
- 47PrivilegedActionException in Java
- 48PropertyVetoException in Java
- 49ReflectiveOperationException in Java
- 50RefreshFailedException in Java
- 51RemarshalException in Java
- 52RuntimeException in Java
- 53SAXException in Java
- 54Java ScriptException
- 55Java ServerNotActiveException
- 56Java SOAPException
- 57Java SQLException
- 58Java TimeoutException
- 59Java TooManyListenersException
- 60Java TransformerException
- 61Java TransformException
- 62Java UnmodifiableClassException
- 63Java UnsupportedAudioFileException
- 64Java UnsupportedCallbackException
- 65Java UnsupportedFlavorException
- 66Java UnsupportedLookAndFeelException
- 67Java URIReferenceException
- 68Java URISyntaxException
- 69Java UserException – Custom Exceptions with Examples
- 70Java XAException
- 71Java XMLParseException – XML Parsing and Exception Handling
- 72Java XMLSignatureException
- 73Java XMLStreamException – StAX Parsing Examples
- 74Java XPathException – Complete Guide with Examples
Java NotBoundException
Introduction to NotBoundException in Java
Java RMI (Remote Method Invocation) allows you to invoke methods on an object running in another JVM or even on another machine. To do this, remote objects are registered with a name in the RMI registry. But what happens when you try to look up a name that hasn’t been registered?
That’s when Java throws a NotBoundException
.
This tutorial is a complete guide to understanding and handling NotBoundException
. We’ll explain it using simple examples, walking through what causes it and how to fix or avoid it. Whether you're managing apples, bananas, or cherries remotely, you'll know how to bind and find them with confidence.
What is NotBoundException?
NotBoundException
is a checked exception in the java.rmi
package. It is thrown when an attempt is made to look up or unbind a name in the RMI registry, but that name hasn’t been registered (or “bound”) yet.
Class Hierarchy
java.lang.Object
↳ java.lang.Throwable
↳ java.lang.Exception
↳ java.io.IOException
↳ java.rmi.RemoteException
↳ java.rmi.NotBoundException
When Does NotBoundException Occur?
This exception is commonly encountered in the following situations:
- Looking up a remote object name that hasn’t been registered in the RMI registry
- Misspelling the name when performing a lookup
- Calling
unbind()
on a name that isn’t registered
Example 1: Triggering NotBoundException
Let’s look at a minimal program that tries to look up an RMI service called "banana", which has not been registered.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class LookupBananaService {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
registry.lookup("banana"); // "banana" is not bound
} catch (NotBoundException e) {
System.out.println("Caught NotBoundException: " + e.getMessage());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Caught NotBoundException: banana
Explanation
The RMI registry is running, but the object named banana
was never registered. Thus, the lookup fails, throwing a NotBoundException
.
How to Fix NotBoundException
The solution is simple: ensure that the remote object is registered with the exact name before you try to look it up.
Example 2: Properly Binding and Looking Up a Remote Object
Step 1: Define the Remote Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FruitService extends Remote {
String getFruitInfo(String name) throws RemoteException;
}
Step 2: Implement the Remote Class
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class FruitServiceImpl extends UnicastRemoteObject implements FruitService {
protected FruitServiceImpl() throws RemoteException {
super();
}
public String getFruitInfo(String name) {
return switch (name.toLowerCase()) {
case "apple" -> "Apple is red and sweet.";
case "banana" -> "Banana is yellow and rich in potassium.";
case "cherry" -> "Cherry is small, red, and tart.";
default -> "Fruit not found.";
};
}
}
Step 3: Register the Service
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class FruitServer {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099); // Start RMI registry
FruitService service = new FruitServiceImpl();
Naming.rebind("fruitService", service);
System.out.println("FruitService bound to registry.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 4: Lookup and Use the Service
import java.rmi.Naming;
public class FruitClient {
public static void main(String[] args) {
try {
FruitService service = (FruitService) Naming.lookup("rmi://localhost/fruitService");
String info = service.getFruitInfo("banana");
System.out.println("Fruit Info: " + info);
} catch (Exception e) {
System.out.println("Lookup failed: " + e.getMessage());
}
}
}
FruitService bound to registry.
Fruit Info: Banana is yellow and rich in potassium.
Best Practices to Avoid NotBoundException
- Always bind remote objects before client lookup
- Verify registry is started before registering or looking up names
- Use consistent and meaningful names for bindings
- Wrap lookup calls in try-catch blocks for graceful handling
Common Mistakes and Fixes
Mistake | Fix |
---|---|
Looking up an unregistered name | Register the object using Naming.bind() or Naming.rebind() |
Wrong hostname or port in getRegistry() | Ensure host and port match the server’s configuration |
Misspelling the name | Double-check and use constants for binding names |
Real-World Analogy
Think of RMI like a library catalog. If you're trying to borrow the book titled "banana" but it was never added to the system, the librarian throws a NotBoundException
. Before making a request, you need to be sure the book is registered and available.
Recap
NotBoundException
is thrown when a name lookup fails in the RMI registry- It’s a checked exception and must be handled
- Always ensure remote objects are bound before lookup
- Start the registry and use consistent names to prevent errors
Conclusion
Java's RMI system makes it easy to build distributed applications, but like a phone call to an unlisted number, looking up a service that hasn’t been registered throws a NotBoundException
. With proper registration, thoughtful naming, and error handling, you can ensure your remote apples, bananas, and cherries are always ready to serve their sweet data across the network.
Comments
Loading comments...