Integrate Simple MATLAB Function into Java Application
This example shows how to invoke a MATLAB® method that generates a magic square in a Java® application.
Files
MATLAB Function Location |
|
Java Code Location |
|
Procedure
Copy the
MagicSquareExample
folder that ships with MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','MagicSquareExample'))
At the MATLAB command prompt, navigate to the new
MagicSquareExample\MagicDemoComp
subfolder in your work folder.Examine the
makesqr.m
function.function y = makesqr(x) y = magic(x);
At the MATLAB command prompt, enter
makesqr(5)
.The output is a 5-by-5 matrix.
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Create a Java package that encapsulates
makesqr.m
by using the Library Compiler app orcompiler.build.javaPackage
.Use the following information for your project:
Package Name magicsquare
Class Name magic
File to Compile makesqr.m
For example, if you are using
compiler.build.javaPackage
, type:buildResults = compiler.build.javaPackage('makesqr.m', ... 'PackageName','magicsquare', ... 'ClassName','magic');
For more details, see the instructions in Generate Java Package and Build Java Application.
Write source code for a Java application that accesses the MATLAB function.
The sample application for this example is in
MagicSquareExample\MagicDemoJavaApp\getmagic.java
.The program does the following:
Creates an
MWNumericArray
array to store the input dataInstantiates a
magic
objectCalls the
makesqr
method, where the first parameter specifies the number of output arguments and the following parameters are passed to the function in order as input argumentsUses a
try
-catch
block to handle exceptionsFrees native resources using
MWArray
methods
In MATLAB, navigate to the
MagicDemoJavaApp
folder.Copy the generated
magicsquare.jar
package into this folder.If you used
compiler.build.javaPackage
, type:copyfile(fullfile('..','MagicDemoComp','magicsquarejavaPackage','magicsquare.jar'))
If you used the Library Compiler, type:
copyfile(fullfile('..','MagicDemoComp','magicsquare','for_testing','magicsquare.jar'))
In a system command window, navigate to the
PlotDemoJavaApp
folder.Compile the Java application using
javac
.On Windows®, execute this command:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic.javaOn UNIX®, execute this command:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may bematlabroot
C:\Program Files\MATLAB\R2024a
.For more details, see Compile and Run MATLAB Generated Java Application.
From the system command prompt, run the application.
On Windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic 5On UNIX, type:
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic 5
The application outputs a 5-by-5 magic square in the command window.
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
To follow up on this example:
Try running the generated application on a different computer.
Try building an installer for the package using
compiler.package.installer
.Try integrating a package that consists of multiple functions.
See Also
compiler.build.javaPackage
| Library Compiler | compiler.package.installer
| mcc
| deploytool