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:
On Windows® systems,
is
on the system path.matlabroot
/runtime/arch
On UNIX systems, LD_LIBRARY_PATH
and DYLD_LIBRARY_PATH
are
set properly. See MATLAB Runtime Path Settings for Run-Time Deployment for
further information on setting the path.
Note
Be sure to enter the following as single, unbroken commands.
Use the following mcc
command to
build the package:
mcc -W 'java:dataTypesComp,dataTypesClass' createEmptyStruct.m updateField.m -v
Compile the server's Java® code:
javac -classpath matlabroot\toolbox\javabuilder\jar\javabuilder.jar; directory_containing_package\dataTypesComp.jar NativeCellStructServer.java
Compile the client's Java code:
javac -classpath matlabroot\toolbox\javabuilder\jar\javabuilder.jar; directory_containing_package\dataTypesComp.jar NativeCellStructClient.java
Prepare to run the server and client applications by opening two command windows—one for client and one for server.
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
Run the client:
java -classpath .;directory_containing_package\dataTypesComp.jar; matlabroot\toolbox\javabuilder\jar\javabuilder.jar NativeCellStructClient
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 #################################