Main Content

Run Client and Server on Same Machine

The following example shows how to run two separate processes to initialize MATLAB® struct arrays.

Note

You do not need the MATLAB Runtime installed on the client side. Return values from the MATLAB Runtime can be automatically converted using the marshalOutputs Boolean in the RemoteProxy class. See the Javadoc API documentation for details at matlabroot/help/toolbox/javabuilder/MWArrayAPI.

  1. Compile the Java® package by issuing the following command at the MATLAB command prompt:

    mcc -W 'java:dataTypesComp,dataTypesClass' createEmptyStruct.m updateField.m -v
    

     createEmptyStruct.m

     updateField.m

  2. Compile the server Java code by issuing the following javac command at your system command prompt. Ensure there are no spaces between javabuilder.jar; and directory_containing_package.

    javac -classpath matlabroot\toolbox\javabuilder\jar\javabuilder.jar; directory_containing_package\dataTypesComp.jar DataTypesServer.java 

    You can find DataTypesServer.java in:

    matlabroot\toolbox\javabuilder\Examples\RMIExamples
    \DataTypes\DataTypesDemoJavaApp

  3. Compile the client Java code by issuing the following javac command at your system command prompt. Ensure there are no spaces between javabuilder.jar; and directory_containing_package.

    javac -classpath matlabroot\toolbox\javabuilder\jar\javabuilder.jar; directory_containing_package\dataTypesComp.jar DataTypesClient.java

  4. Run the client and server as follows:

    1. Open two command windows.

    2. If running Windows®, ensure that matlabroot/runtime/arch is defined on the system path. If running UNIX®, ensure LD_LIBRARY_PATH and DYLD_LIBRARY_PATH are set properly.

    3. Run the server by issuing the following java command at the system command prompt. Ensure there are no spaces between dataTypesComp.jar; and matlabroot.

      java -classpath .;directory_containing_package\dataTypesComp.jar; matlabroot\toolbox\javabuilder\jar\javabuilder.jar -Djava.rmi.server.codebase="file:///matlabroot/toolbox/javabuilder/jar/javabuilder.jar file:///directory_containing_package/dataTypesComp.jar" DataTypesServer
      
    4. Run the client by issuing the following java command at the system command prompt. Ensure there are no spaces between dataTypesComp.jar; and matlabroot.

      java -classpath .;directory_containing_package\dataTypesComp.jar; matlabroot\toolbox\javabuilder\jar\javabuilder.jar DataTypesClient

    You can find DataTypesClient.java in: matlabroot\toolbox\javabuilder\Examples\RMIExamples\DataTypes\DataTypesDemoJavaApp.

    If successful, the following output appears in the Command Window running the server:

    Please wait for the server registration notification.            
                Server registered and running successfully!!
    
                EVENT 1: Initializing the structure on server
                         and sending it to client:
                         Initialized empty structure:
    
                       Name: []
                    Address: []
    
                ##################################
    
                EVENT 3: Partially initialized structure as received by server:
    
                       Name: []
                    Address: [1x1 struct]
    
                Address field as initialized from the client:
    
                    Street: '3, Apple Hill Drive'
                      City: 'Natick'
                     State: 'MA'
                       Zip: '01760'
    
                ##################################
    
                EVENT 4: Updating 'Name' field before 
                         sending the structure back to the client:
    
                      Name: 'The MathWorks'
                   Address: [1x1 struct]
    
                ##################################
    
    If successful, the following output appears in the Command Window running the client:
     Running the client application!!
    
                EVENT 2: Initialized structure as received in client applications:
    
                       Name: []
                    Address: []
    
                Updating the 'Address' field to :
    
                    Street: '3, Apple Hill Drive'
                      City: 'Natick'
                     State: 'MA'
                       Zip: '01760'
    
                #################################
    
    
                EVENT 5: Final structure as received by client:
    
                       Name: 'The MathWorks'
                    Address: [1x1 struct]
    
                Address field:
    
                    Street: '3, Apple Hill Drive'
                      City: 'Natick'
                     State: 'MA'
                       Zip: '01760'
    
                #################################