This example shows how to create a Java® package using a MATLAB® function. You can then pass the generated package to the developer, who is responsible for integrating it into an application.
To compile a Java package from MATLAB code:
In MATLAB, examine the MATLAB code that want to deploy as a Java package.
Open makesqr.m
.
function y = makesqr(x)
y = magic(x);
At the MATLAB command prompt, enter makesqr(5)
.
The output appears as follows:
ans = 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
Open the Library Compiler app.
On the toolstrip, select the Apps tab.
Click the arrow at the far right of the tab to open the apps gallery.
Click Library Compiler.
In the Application Type section of the toolstrip, select Java Package from the list.
Specify the MATLAB functions you want to deploy.
In the Exported Functions section of the toolstrip, click the plus button.
In the file explorer that opens, locate and select the makesqr.m
file.
makesqr.m
is located in
.matlabroot
\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp
Click Open to select the file, and close the file explorer.
makesqr.m is added to the list of exported files and a minus button appears under the plus button. In addition, makesqr is set as:
the library name
the package name
Verify that the function defined in makesqr.m
is
mapped into Class1
.
In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.
This option creates an application installer that automatically downloads the MATLAB Runtime and installs it along with the deployed package.
Click Package.
Select the Open output folder when process completes check box.
Verify that the generated output contains:
for_redistribution
— A
folder containing the installer to distribute the package
for_testing
— A folder
containing the raw generated files to create the installer
for_redistribution_files_only
—
A folder containing only the files needed to redistribute the package
PackagingLog.txt
— A log
file generated by the compiler
Click Close on the Package window.
Open the for_redistribution
folder.
Run the installer.
In the folder containing the generated JAR files, create
a new file called getmagic.java
.
Using a text editor, open getmagic.java
.
Paste the following code into the file.
import com.mathworks.toolbox.javabuilder.*; import makesqr.*; class getmagic { public static void main(String[] args) { MWNumericArray n = null; Object[] result = null; Class1 theMagic = null; if (args.length == 0) { System.out.println("Error: must input a positive integer"); return; } try { n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE); theMagic = new Class1(); result = theMagic.makesqr(1, n); System.out.println(result[0]); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { MWArray.disposeArray(n); MWArray.disposeArray(result); theMagic.dispose(); } } } |
Compile the Java application using javac
.
javac -classpath "mcrroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar .\getmagic.java
On UNIX® platforms, use colon (:
) as
the class path delimiter instead of semicolon (;
).
mcrroot
is the path to where the MATLAB Runtime is
installed on your system. If you have MATLAB installed on your
system instead, you can use the path to your MATLAB installation.
From the system's command prompt, run the application.
java -classpath .;"mcrroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar getmagic 5 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
You must be sure to place a dot (.
) in the
first position of the class path. If it not, you get a message stating
that Java cannot load the class.
On UNIX platforms, use colon (:
) as
the class path delimiter instead of semicolon (;
).
mcrroot
is the path to where the MATLAB Runtime is
installed on your system. If you have MATLAB installed on your
system instead, you can use the path to your MATLAB installation.