# org.omg.CosNaming.NamingContextPackage

***

**1. Creating and Binding a Name to an Object**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the object
        NameComponent[] name = { new NameComponent("MyObject", "Object") };

        // Bind the name to the object
        rootContext.bind(name, orb.create_reference("MyObject"));
    }
}
```

**2. Resolving a Name to an Object**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the object
        NameComponent[] name = { new NameComponent("MyObject", "Object") };

        // Resolve the name to the object
        Object object = rootContext.resolve(name);
    }
}
```

**3. Creating a New Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the naming context
        NameComponent[] name = { new NameComponent("MyContext", "Context") };

        // Create the new naming context
        NamingContext newContext = rootContext.create_context(name);
    }
}
```

**4. Destroying a Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the naming context
        NameComponent[] name = { new NameComponent("MyContext", "Context") };

        // Destroy the naming context
        rootContext.destroy_context(name);
    }
}
```

**5. Listing the Contents of a Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // List the contents of the naming context
        BindingIterator iterator = rootContext.list(100);
        while (iterator.next_one()) {
            System.out.println(iterator.binding().binding_name[0].id);
        }
    }
}
```

**6. Removing a Binding from a Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] name = { new NameComponent("MyObject", "Object") };

        // Remove the binding from the naming context
        rootContext.unbind(name);
    }
}
```

**7. Renaming a Binding in a Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] newName = { new NameComponent("MyNewObject", "Object") };

        // Rename the binding in the naming context
        rootContext.rebind(newName, rootContext.resolve(name));
    }
}
```

**8. Searching for Objects in a Naming Context**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] name = { new NameComponent("MyObject", "Object") };

        // Search for the object in the naming context
        BindingIterator iterator = rootContext.find_binding(name);
        while (iterator.next_one()) {
            System.out.println(iterator.binding().binding_name[0].id);
        }
    }
}
```

**9. Binding an Object to Multiple Names**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] name1 = { new NameComponent("MyObject1", "Object") };
        NameComponent[] name2 = { new NameComponent("MyObject2", "Object") };

        // Bind the object to multiple names
        rootContext.bind(name1, orb.create_reference("MyObject"));
        rootContext.bind(name2, orb.create_reference("MyObject"));
    }
}
```

**10. Resolving a Name to Multiple Objects**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] name1 = { new NameComponent("MyObject1", "Object") };
        NameComponent[] name2 = { new NameComponent("MyObject2", "Object") };

        // Resolve the name to multiple objects
        Object[] objects = rootContext.resolve_str_list(new String[] { name1[0].id, name2[0].id });
    }
}
```

**11. Using a Name Service as a Directory**

```java
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingExample {
    public static void main(String[] args) {
        // Initialize the ORB
        ORB orb = ORB.init(args, null);

        // Get the root naming context
        NamingContextExt rootContext = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

        // Create a new name for the binding
        NameComponent[] name = { new NameComponent("MyObject", "Object") };

        // Bind the object to the name
        rootContext.bind(name, orb.create_reference("MyObject"));

        // Create a new client
        Client client = new Client();

        // Use the name service to lookup the object
        Object object = client.lookup(rootContext, "MyObject");

        // Use the object

```
