Run Client and Server Using RMI
This example shows how to implement RMI to run two separate processes that initialize MATLAB® struct arrays. The client and the server run on the same machine.
To implement RMI with a client on one machine and a server on another, use the procedure in this example and:
Change how the server is bound to the system registry.
Redefine how the client accesses the server.
RMI Prerequisites
To run this example, your environment must meet the following prerequisites:
Install MATLAB Compiler SDK™ on the development machine.
Install a supported version of the Java® Development Kit (JDK™) on the development machine. For more information, see Configure Your Environment for Generating Java Packages.
Install MATLAB Runtime on the web server. For details, see Download and Install MATLAB Runtime.
Ensure that your web server is capable of running accepted Java frameworks like J2EE.
Install the
javabuilder.jarlibrary () into your web server’s common library folder.matlabroot/toolbox/javabuilder/jar/javabuilder.jarIf your implementation uses separate client machines, they also need
javabuilder.jar, since it contains thecom.mathworks.extern.javapackage.
Note
You do not need MATLAB Runtime installed on the client side. Return values from MATLAB Runtime can be automatically converted using the boolean
marshalOutputs in the RemoteProxy class. For details, see
the Javadoc API documentation in
.matlabroot/help/toolbox/javabuilder/MWArrayAPI
Files
| MATLAB Function Location | |
| Java Code Location | |
Procedure
Copy the
DataTypesfolder from MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','RMIExamples','DataTypes'))
At the MATLAB command prompt, navigate to the new
DataTypes\DataTypesDemoCompsubfolder in your work folder.Examine the MATLAB functions
createEmptyStruct.mandupdateField.m.Generate the Java package using
compiler.build.javaPackageby issuing the following command at the MATLAB command prompt:compiler.build.javaPackage({'createEmptyStruct.m','updateField.m'}, ... 'PackageName','dataTypesComp', ... 'ClassName','dataTypesClass', ... 'Verbose','on');For more details, see the instructions in Generate Java Package and Build Java Application.
At your system command prompt, navigate to the
DataTypes\DataTypesDemoJavaAppfolder.Compile the server Java code by issuing one of the following
javaccommands at your system command prompt.On Windows®, type:
javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar;path\to\dataTypesComp.jar" DataTypesServer.javaOn UNIX®, type:
javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar:path/to/dataTypesComp.jar" DataTypesServer.java
Note
Replace
with the path to your MATLAB or MATLAB Runtime installation folder.matlabrootCompile the client Java code by issuing one of the following
javaccommands at your system command prompt.On Windows, type:
javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar;path\to\dataTypesComp.jar DataTypesClient.javaOn UNIX, type:
javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar:path/to/dataTypesComp.jar" DataTypesClient.java
Run Client and Server
Run the client and server as follows:
Open two command windows—one for the server and one for the client.
In each window, navigate to the folder that contains
DataTypesServer.javaorDataTypesClient.java, respectively.Run the server by issuing one of the following
javacommands in a single line at the system command prompt.On Windows, type:
java -classpath .;"path\to\dataTypesComp.jar;matlabroot\toolbox\javabuilder\jar\javabuilder.jar" -Djava.rmi.server.codebase="file:///matlabroot\toolbox\javabuilder\jar\javabuilder.jar file:///path\to\dataTypesComp.jar" DataTypesServerOn UNIX, type:
java -classpath .:"path/to/dataTypesComp.jar;matlabroot/toolbox/javabuilder/jar/javabuilder.jar" -Djava.rmi.server.codebase="file:///matlabroot/toolbox/javabuilder/jar/javabuilder.jar file:///path/to/dataTypesComp.jar" DataTypesServer
In the second command window, run the client by issuing one of the following
javacommands in a single line.On Windows, type:
java -classpath .;"path/to/\dataTypesComp.jar;matlabroot\toolbox\javabuilder\jar\javabuilder.jar" DataTypesClientOn UNIX, type:
java -classpath .:"path/to/dataTypesComp.jar;matlabroot/toolbox/javabuilder/jar/javabuilder.jar" -Djava.rmi.server.codebase="file:///matlabroot/toolbox/javabuilder/jar/javabuilder.jar file:///path/to/dataTypesComp.jar" DataTypesClient
If the commands are 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]
##################################
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'
#################################
Note
For more examples of RMI implementation, see the files in
.matlabroot/toolbox/javabuilder/Examples/RMIExamples
