Hello World

This section contains:
The IDL for a simple "Hello World" program
A transient server that creates an object and registers it in a naming context
An application client that, knowing the object's name, retrieves a reference for it from the naming context, and invokes it
An applet that does the same

Instructions for compiling and running the example are provided, and source code is also included.

Note: The Hello World example is provided with pre-built stub and skeleton files. However, to generate these files yourself and take full advantage of this programming guide, you will need to download the idltojava compiler.

Example IDL

The following example OMG IDL describes a CORBA object whose single sayHello() operation returns a string.

module HelloApp
{
    interface Hello
    {
        string sayHello();
    };
};

Example Server

The example server consists of two classes, the servant and the server. The servant, HelloServant, is the implementation of the Hello IDL interface; each Hello instance is implemented by a HelloServant instance. The servant is a subclass of _HelloImplBase, which is generated by the idltojava compiler from the example IDL. The servant contains one method for each IDL operation, in this example, just the sayHello() method. Servant methods are just like ordinary Java methods; the extra code to deal with the ORB, with marshaling arguments and results, and so on, is provided by the server and the stubs.

The server class has the server's main() method, which:

Creates an ORB instance
Creates a servant instance (the implementation of one CORBA Hello object) and tells the ORB about it
Gets a CORBA object reference for a naming context in which to register the new CORBA object
Registers the new object in the naming context under the name "Hello"
Waits for invocations of the new object
// Copyright and License 

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
 
class HelloServant extends _HelloImplBase 
{
    public String sayHello()
    {
	return "\nHello world !!\n";
    }
}

 
public class HelloServer {
 
    public static void main(String args[])
    {
	try{
	    // create and initialize the ORB
	    ORB orb = ORB.init(args, null);
 
	    // create servant and register it with the ORB
	    HelloServant HelloRef = new HelloServant();
	    orb.connect(HelloRef);
 
	    // get the root naming context
	    org.omg.CORBA.Object objRef = 
		orb.resolve_initial_references("NameService");
	    NamingContext ncRef = NamingContextHelper.narrow(objRef);
 
	    // bind the Object Reference in Naming
	    NameComponent nc = new NameComponent("Hello", "");
	    NameComponent path[] = {nc};
	    ncRef.rebind(path, HelloRef);
 
	    // wait for invocations from clients
            java.lang.Object sync = new java.lang.Object();
            synchronized (sync) {
                sync.wait();
            }
 
	} catch (Exception e) {
	    System.err.println("ERROR: " + e);
	    e.printStackTrace(System.out);
	}
    }
}
 

Example Application Client

The example application client that follows:

Creates an ORB
Obtains a reference to the naming context
Looks up "Hello" in the naming context and receives a reference to that CORBA object
Invokes the object's sayHello() operation and prints the result
// Copyright and License 
 
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
 
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
            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);
	}
    }
}

 

Example Applet Client

The example applet client that follows:

Creates an ORB
Obtains a reference to the naming context
Looks up "Hello" in the naming context and receives a reference to that CORBA object
Invokes the object's sayHello() operation and prints the result
// Copyright and License 

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.awt.Graphics;
 
public class HelloApplet extends java.applet.Applet 
{
    public void init() {
	try {
	    // create and initialize the ORB
            ORB orb = ORB.init(this, 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
            NameComponent nc = new NameComponent("Hello", "");
            NameComponent path[] = {nc};
            Hello HelloRef = HelloHelper.narrow(ncRef.resolve(path));

            // call the Hello server object and print results
            message = HelloRef.sayHello();
 
	} catch (Exception e) {
	    System.out.println("HelloApplet exception: " + e.getMessage());
	    e.printStackTrace(System.out);
        }
    }
 

    public void paint(Graphics g) 
    {
	g.drawString(message, 25, 50);
    }
 
    String message = "";
}
 

Building and Running Hello World

The source code for the Hello World example is located in the examples/hello directory. The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary. Note that for ports below 1024, you need root access on UNIX machines, and administrator privileges on Windows95 and NT. Note also that the instructions use a slash (/) for path names. Windows95 and NT users should subtitute a backslash (\).

Note: The Hello World example is provided with pre-built stub and skeleton files. However, to generate these files yourself, you will need to download the idltojava compiler.
Change to the directory that contains the file Hello.idl
	cd docs/guide/idl/examples/hello
Run idltojava on the IDL file to create stubs and skeletons:
	idltojava  Hello.idl
Compile the .java files, including the stubs and skeletons:
	javac *.java HelloApp/*.java
Make sure the name server is running:
	tnameserv -ORBInitialPort 1050&
Start the Hello server:
	java HelloServer -ORBInitialPort 1050
Run the Hello application client from a different shell than the server:
	java HelloClient -ORBInitialPort 1050


Home

Fundamentals

Programming

References

Tutorial

Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.