# Getting Started with Java IDL: Developing a Client Application

This topic introduces the basics of writing a CORBA client application. To create a CORBA client applet, follow the link. Included in this lesson are:

1. Creating HelloClient.java
2. Understanding HelloClient.java
3. Compiling HelloClient.java

To see a completed version of `HelloClient.java`, follow the link.

## Creating HelloClient.java

To create `HelloClient.java`,

1. Start your text editor and create a file named `HelloClient.java` in your main project directory, `Hello`.

2. Enter the following code for `HelloClient.java` in the text file. The following section, Understanding HelloClient.java, explains each line of code in some detail.

       import HelloApp.*;           // The package containing our stubs.
       import org.omg.CosNaming.*;  // HelloClient will use the naming service.
       import org.omg.CORBA.*;      // All CORBA applications need these classes.

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

             // Create and initialize the ORB
             ORB orb = ORB.init(args, null);

             // Get the root naming context
             org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
             NamingContext ncRef = NamingContextHelper.narrow(objRef);

             // Resolve the object reference in naming
             // make sure there are no spaces between ""
             NameComponent nc = new NameComponent("Hello", "");
             NameComponent path[] = {nc};
             Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));

             // Call the Hello server object and print results
             String Hello = helloRef.sayHello();
             System.out.println(Hello);

           } catch(Exception e) {
               System.out.println("ERROR : " + e);
               e.printStackTrace(System.out);
             }
         }
       }

3. Save and close `HelloClient.java`.

## Understanding HelloClient.java

This section explains each line of `HelloClient.java`, describing what the code does, as well as why it is needed for this application.

### Performing Basic Setup

The basic shell of a CORBA client is the same as many Java applications: You import required library packages, declare the application class, define a `main` method, and handle exceptions.

#### Importing Required Packages

First, we import the packages required for the client class:

    import HelloApp.*;           // The package containing our stubs.
    import org.omg.CosNaming.*;  // HelloClient will use the naming service.
    import org.omg.CORBA.*;      // All CORBA applications need these classes.

#### Declaring the Client Class

The next step is to declare the client class:

    public class HelloClient
    {
      // The main() method goes here.
    }

#### Defining a `main()` Method

Every Java application needs a `main()` method. It is declared within the scope of the `HelloClient` class, as follows:

      public static void main(String args[])
      {
        // The try-catch block goes here.
      }

#### Handling CORBA System Exceptions

Because all CORBA programs can throw CORBA system exceptions at runtime, all of the `main()` functionality is placed within a try-catch block. CORBA programs throw system exceptions whenever trouble occurs during any of the processes (marshaling, unmarshaling, upcall) involved in invocation.

Our exception handler simply prints the name of the exception and its stack trace to standard output so you can see what kind of thing has gone wrong.

The try-catch block is set up inside `main()`,

        try{

          // Add the rest of the HelloClient code here.

        } catch(Exception e) {
            System.out.println("ERROR : " + e);
            e.printStackTrace(System.out);
          }

### Creating an ORB Object

A CORBA client needs a local ORB object to perform all of its marshaling and IIOP work. Every client instantiates an `org.omg.CORBA.ORB` object and initializes it by passing to the object certain information about itself.

The ORB variable is declared and initialized inside the try-catch block.

          ORB orb = ORB.init(args, null);

The call to the ORB's `init()` method passes in your application's command line arguments, allowing you to set certain properties at runtime.

### Finding the Hello Server

Now that the application has an ORB, it can ask the ORB to locate the actual service it needs, in this case the Hello server. There are a number of ways for a CORBA client to get an initial object reference; our client application will use the COS Naming Service specified by OMG and provided with Java IDL. See Using Stringified Object References for information on how to get an initial object reference when there is no naming service available.

#### Obtaining the Initial Naming Context

The first step in using the naming service is to get the initial naming context. In the try-catch block, below your ORB initialization, you call `orb.resolve_initial_references()` to get an object reference to the name server:

          org.omg.CORBA.Object objRef =
                orb.resolve_initial_references("NameService");

The string "NameService" is defined for all CORBA ORBs. When you pass in that string, the ORB returns the initial naming context, an object reference to the name service.

#### Narrowing the Object Reference

As with all CORBA object references, `objRef` is a generic CORBA object. To use it as a `NamingContext` object, you must narrow it to its proper type.

          NamingContext ncRef = NamingContextHelper.narrow(objRef);

Here we see the use of an `idlj`-generated helper class, similar in function to `HelloHelper`. The `ncRef` object is now an `org.omg.CosNaming.NamingContext` and you can use it to access the naming service and find other services. You will do that in the next step.

#### Finding a Service in Naming

Names can have different structures depending upon the implementation of the naming service. Consequently, CORBA name servers handle complex names by way of `NameComponent` objects. Each `NameComponent` holds a single part, or element, of the name. An array of `NameComponent` objects can hold a fully specified path to an object on any computer file or disk system.

To find the Hello server, you first need a `NameComponent` to hold an identifying string for the Hello server.

          NameComponent nc = new NameComponent("Hello", "");

This statement sets the `id` field of `nc` to "Hello" and the `kind` field to an empty string. Be sure this is an empty string, do not enter a space between "".

Because the path to the `Hello` object has just one element, we have created a single-element array out of `nc`. The `NamingContext.resolve()` method requires this array for its work:

          NameComponent path[] = {nc};

Finally, we pass `path` to the naming service's `resolve()` method to get an object reference to the Hello server and narrow it to a `Hello` object:

          Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));

Here you see the `HelloHelper` helper class at work. The `resolve()` method returns a generic CORBA object as you saw above when locating the name service itself. Therefore, you immediately narrow it to a `Hello` object, which is the object reference you need to perform the rest of your work.

### Invoking the `sayHello()` Operation

CORBA invocations look like a method call on a local object. The complications of marshaling parameters to the wire, routing them to the server-side ORB, unmarshaling, and placing the upcall to the server method are completely transparent to the client programmer. Because so much is done for you by generated code, invocation is really the easiest part of CORBA programming.

          String Hello = helloRef.sayHello();

Finally, we print the results of the invocation to standard output:

          System.out.println(Hello);

## Compiling HelloClient.java

Now we will compile `HelloClient.java` so that we can correct any errors before continuing with this tutorial.

Windows users note that you should substitute backslashes (\) for the slashes (/) in all paths in this document.

To compile `HelloClient.java`,

1. Change to the `Hello` directory.

2. Run the Java compiler on `HelloClient.java`:

       javac HelloClient.java HelloApp/*.java

3. Correct any errors in your file and recompile if necessary. (You can copy the file from `HelloClient.java` if you have trouble finding your typographical errors).

4. The `HelloClient.class` is generated to the `Hello` directory.

### Running the Client Application

We need to create and compile the Hello server before we can successfully run the Hello client application. Running the Hello World application is covered in Running the Hello World Application.

## For More Information

Developing Clients
  Covers topics of interest to CORBA client programmers
Exceptions: System Exceptions
  Explains how CORBA system exceptions work and provides details on the minor codes of Java IDL's system exceptions
Initialization: System Properties
  Explains what properties can be passed to the ORB at initialization
Naming Service
  Covers the COS Naming Service in greater detail

Previous: Writing the IDL interface
Next: Developing a Client Applet or Next: Developing the Hello World Server
Tutorial home: Getting Started with Java IDL | `HelloClient.java`

Home

Copyright © 1996, 1997 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA. 94043-1100 USA., All rights reserved.