Hello World Java Client

Our source for the Java client is contained in HelloClient.java. Its semantics are identical to the C++ client described in more detail here. Speaking of more detail, let's look at the Java client. (I'm assuming you've already looked closely at the server.)


Our HelloClient contains only a main method with all of its functionality wrapped within one try block.
public class HelloClient 
{
    public static void main( String args[] ) {
        try {
	
As with all CORBA applications, we must initialize our ORB. In Java, we do this by calling the ORB static method, init. Its first parameter is the array of command line arguments passed to main. As with the C++ ORB_init function, it removes from args all of the ORB-specific options. The second parameter is for application-specific properties: they allow a different vendor's ORB implementation to be "plugged in."
                // Initialize the orb
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
	
In Java, neither the Java interpreter nor the classname passed to it appear in the args array passed to main(). Because we expect the IOR (Inter-operable Object Reference) to be passed as the only parameter, we expect args to contain one and only one argument. We wouldn't have been able to pass any ORB-specific parameters had we checked the length of args prior to calling init().
                // Expect IOR as the only command line argument
            if (args.length != 1) // class name doesn't count in Java
            {
                throw new Exception("Usage: java HelloClient IOR");
            }
	
We call the ORB method, string_to_object, to convert (or de-stringify) the IOR passed on the command line.
                // Destringify the IOR
            org.omg.CORBA.Object obj = orb.string_to_object( args[0]);
	
The string_to_object function could throw an exception if the IOR is mangled somehow, or it might return a nil reference. Here's a neat convenience about the CORBA Java language mapping: a special "nil" reference isn't necessary, as it is with C++, so there's no need for the cumbersome CORBA::is_nil() function or the static _nil() method created for each IDL interface. In Java, we may simply use null!
            if (obj == null) 
            {
                throw new Exception("null World reference");
            }
	
Now we must narrow the Object to something to which we can say hello: a World reference. If narrow fails, it throws an exception; it will only return null if passed a null, and we already checked for that. Notice that narrow() is a static method on the WorldHelper class. We need a "helper" class because World itself is not a Java class: it's a Java interface and therefore may not define any methods (it can only declare them).
            World world = WorldHelper.narrow(obj);
	
Finally, we may say hello...
                // Say hello to the world
            String s = world.hello();
	
We'll print the World's response to stdout.
                // Print the response
            System.out.println("World said \"" + s + "\"");
	
We're done. All that's left is to catch any exceptions that might've been thrown in the try block. We're not specific about which ones.
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}
	
I'm sure you must be ready to build and run the Java client and server by now.

Back to... [ Corba Tutorials ]


Jim Crossley
Last modified: Fri Mar 26 19:50:18 EST 1999