Use Native Java Cell and Struct Arrays

Before You Run the Example

Before you run this example, note the following:

  • This example demonstrates how to implement RMI when the client and the server are running on the same machine. See Run Client and Server on Separate Machines if you would like to do otherwise.

  • On UNIX®, use : as the path separator in calls to java and javac. Use ; as a path separator on Windows.

  • Only update the server system path with the location of the MATLAB® Runtime. The client does not need access to the MATLAB Runtime.

  • This example is shipped in the matlab\toolbox\javabuilder\Examples\RMIExamples\NativeCellStruct directory.

  • Ensure that:

Running the Example

Note

Be sure to enter the following as single, unbroken commands.

  1. Use the following mcc command to build the package:

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

  2. Compile the server's Java® code:

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

  3. Compile the client's Java code:

    javac -classpath matlabroot\toolbox\javabuilder\jar\javabuilder.jar;
    directory_containing_package\dataTypesComp.jar 
    NativeCellStructClient.java 
    
  4. Prepare to run the server and client applications by opening two command windows—one for client and one for server.

  5. Run the server:

    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" 
    NativeCellStructServer
  6. Run the client:

    java -classpath .;directory_containing_package\dataTypesComp.jar;
    matlabroot\toolbox\javabuilder\jar\javabuilder.jar NativeCellStructClient
  7. If your application has run successfully, the output will display as follows:

    • Server output:

       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]
      
      
                  ##################################
      
      
      
         

    • Client output:

      Running the client application!!
      
                  EVENT 2: Initialized structure as received in client 
                                                          applications:
      
                           1x1 struct array with fields:
                              Name
                              Address
      
                      Updating the 'Address' field to :
      
                           1x1 struct array with fields:
                              Street
                              City
                              State
                              Zip
      
                  #################################
      
                  EVENT 5: Final structure as received by client:
      
                           1x1 struct array with fields:
                              Name
                              Address
      
                      Address field:
      
                           1x1 struct array with fields:
                              Street
                              City
                              State
                              Zip
      
                  #################################