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
MagicSquareExamplefolder 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\MagicDemoCompsubfolder in your work folder.Examine the
makesqr.mfunction.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 9Create a Java package that encapsulates
makesqr.mby using the Library Compiler app orcompiler.build.javaPackage.Use the following information for your project:
Package Name magicsquareClass Name magicFile to Compile makesqr.mFor 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
MWNumericArrayarray to store the input dataInstantiates a
magicobjectCalls the
makesqrmethod, 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-catchblock to handle exceptionsFrees native resources using
MWArraymethods
In MATLAB, navigate to the
MagicDemoJavaAppfolder.Copy the generated
magicsquare.jarpackage 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
PlotDemoJavaAppfolder.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 bematlabrootC:\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 9To 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
