我ntegrate SimpleMATLABFunction IntoJavaApplication
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.functiony = 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 theLibrary Compilerapp orcompiler.build.javaPackage
.Use the following information for your project:
包名 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 inGenerate 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 data我nstantiates 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
我n MATLAB, navigate to the
MagicDemoJavaApp
folder.Copy the generated
magicsquare.jar
package into this folder.我f you used
compiler.build.javaPackage
, type:copyfile(fullfile('..','MagicDemoComp','magicsquarejavaPackage','magicsquare.jar'))
我f you used the Library Compiler, type:
copyfile(fullfile('..','MagicDemoComp','magicsquare','for_testing','magicsquare.jar'))
我n 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 orMATLAB Runtimeinstallation folder. For example, on Windows, the path may bematlabroot
C:\Program Files\MATLAB\R2022a
.For more details, seeCompile 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
libraryCompiler
|compiler.build.javaPackage
|compiler.package.installer
|mcc
|deploytool