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 or .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\r2023a.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 .
try integrating a package that consists of multiple functions.
see also
| | | |