Main page / Java Connectivity page

Getting started - A simple "Hello" example


Description:
	Build a small 'hello world' console application:
        	- Server written in Java with RMI/IIOP
        	- Client in .Net C#

Function:
	The .Net client calls a function provided by a Java server (The function has the
	signature 'string hello(string)') and displays the result.

Goal:
	Write a Java RMI Server, create IDL from Java, generate C# code from IDL
	and write a .Net mobile client application.

Source:
	MinCor\DemoJava\Hello



Example


Step 1. Write the Java Server

	a.) Remote interface (Hello\Greetings.java):
    	
        ...
	    public interface Greetings extends Remote  {

	        String hello( String name)
		        throws RemoteException;
        }
	    

    b.) Implementation to the interface (Hello\GreetingsImpl.java):
        

        ...
        public class GreetingsImpl extends PortableRemoteObject implements Greetings{

            ...
	        public String hello( String name ) throws RemoteException {
		        System.out.println("Greeting from" + name);
		        return "Greetings to " + name;
	        }
        }
        

    c.) The Java main (Server.java):

        Create a instance of the remote object:
        
            GreetingsImpl oGreetingsImpl = new GreetingsImpl();
        

        Connect the name service:
        
            Context initialNamingContext = new InitialContext();
        

        Publish the reference with the name service. Use the 'HelloJavaServer' as
        the name by which the object is published:
        
            initialNamingContext.rebind("HelloJavaServer", oGreetingsImpl);
        



    d.) Compile the Java project:
		
        > mkdir classes
        > javac -classpath . -d classes Server.java
        > rmic -iiop -classpath .\classes -d classes Hello.GreetingsImpl
        > cd classes
        > jar cvf ..\Server.jar .
        > cd ..
        

Step 2. Generate the IDL files
        
        > mkdir idl
        > MCjava2idl -cp Server.jar -v -p -d ..\IDL "Hello.Greetings"
        

Step 3. Generate .Net code form the IDL-File
        
        > MCidl2cs -all -d ..\DotNetClient\Generated ..\IDL\Hello.idl
        

Step 4. Write the .Net Client

    a.) Create a mobile application.

    b.) Add a reference to 'MinCor\bin\MinCorCF.DLL' and
                        to 'MinCor\bin\JavaBuildinTypesCF.DLL'.

    c.) Add the generated file: Generated\Hello.cs.

    d.) The Client (ClientImpl.cs):


        Define the connection parameter (server address & port) for the name service. 
        Note, that 'localhost' is just a pattern for later replacement with a passed in address string:
        
        private static string[] m_strORBInit = {"-ORBInitRef NameService=corbaloc:iiop:1.2@localhost:1050/NameService"};
        
        //...
        m_strORBInit[0] = m_strORBInit[0].Replace("localhost", a_strIpAddr);
        


        Initialize the CORBA ORB:
        
        Middsol.CORBA.ORB oOrb = Middsol.CORBA._ORB.init( m_strORBInit, null );
        


        Connect to the name service:
        
        Middsol.CosNaming.NamingContextExt oNsCtx 
                = Middsol.CosNaming.NamingContextExtHelper.narrow( oOrb.resolve_initial_references("NameService"))
        


        Use the name service to get an object reference to the remote object.
        It is assumed that the remote object is known as 'HelloJavaServer' in the name service:
        
        Hello.Greetings oGreetings = Hello.GreetingsHelper.narrow( oNsCtx.resolve_str("HelloJavaServer"));
        

        Make a call to the remote object:
        
        string strRet = oGreetings.hello( "Middsol" );
        a_oFrmClt.writeLog(strRet);
        

    e.) Implement a hosting frame (HelloClt.cs)



Step 5. Run the example

    a.) Start the sun name service on port 1050:

        > start orbd -ORBInitialPort 1050

    b.) Start the java server by using JNDI CosNaming and
        work with a name service running on 'localhost' at port '1050':

        > start java  -cp .;Server.jar -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:1050  Server

    c.) Copy the .Net Client to the target machine.
    
    d.) Start the .Net Client.