Generate Java Package and Build Java Application
Supported platforms: Windows®, Linux®, Mac
This example shows how to create a Java® package from a MATLAB® function and integrate the package into a Java application generated with MATLAB Compiler SDK™.
Prerequisites
Verify that you have a version of Java installed that is compatible with MATLAB Compiler SDK. For information on supported Java versions, see MATLAB Interfaces to Other Languages.
For information on configuring your development environment after installation, see Configure Your Environment for Generating Java Packages.
End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.
Create Function in MATLAB
In MATLAB, examine the MATLAB code that you want to package. For this example, create a function named
makesqr.m
that contains the following code.
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 Java Package Using compiler.build.javaPackage
Build a Java package using a programmatic approach. Alternatively, if you want to create a Java package using a graphical interface, see Create Java Package Using Java Package Compiler App.
Create a MATLAB sample script that calls your function. Save the following code in a sample file named
makesqrSample1.m
.% Sample script to demonstrate execution of function y = makesqr(x) x = 5; y = makesqr(x);
During packaging, MATLAB Compiler SDK uses the sample MATLAB script to generate a sample application in the target language. For more information, see Create Sample Code to Call Exported Function.
Build the Java package using the
compiler.build.javaPackage
function and themakesqr.m
file that you created earlier. Use name-value arguments to add the sample file and enable verbose output.buildResults = compiler.build.javaPackage('makesqr.m', ... 'SampleGenerationFiles','makesqrSample1.m', ... 'Verbose','on');
You can specify additional options in the
compiler.build
command by using name-value arguments. For details, seecompiler.build.javaPackage
.The
compiler.build.Results
objectbuildResults
contains information on the build type, generated files, included support packages, and build options.The function generates the following files and folders within a folder named
makesqrjavaPackage
in your working folder.classes
— Folder that contains the Java class files and the deployable archive CTF file.doc
— Folder that contains HTML documentation for all classes in the package.example
— Folder that contains Java source code files.samples
— Folder that contains the Java sample driver filemakesqrSample1.java
.GettingStarted.html
— File that contains information on integrating your package.includedSupportPackages.txt
— Text file that lists all support files included in the package.makesqr.jar
— Java archive file.mccExcludedFiles.log
— Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see Functions Not Supported For Compilation.readme.txt
— Text file that contains information on deployment prerequisites and the list of files to package for deployment.requiredMCRProducts.txt
— Text file that contains product IDs of products required by MATLAB Runtime to run the application.unresolvedSymbols.txt
— Text file that contains information on unresolved symbols.
Note
The generated package does not include MATLAB Runtime or an installer. To create an installer using the
buildResults
object, seecompiler.package.installer
.
Compile and Run MATLAB Generated Java Application
After creating your Java package, you can call it from a Java application. This example uses the sample Java code generated during packaging. You can use this sample Java application code as a guide to write your own application.
Copy and paste the generated Java file
makesqrSample1.java
from thesamples
folder into the folder that contains themakesqr.jar
package.At the system command prompt, navigate to the folder that contains
makesqrSample1.java
andmakesqr.jar
.Compile the application using
javac
. In the classpath argument, you specify the paths tojavabuilder.jar
, which contains thecom.mathworks.toolbox.javabuilder
package, and your generated Java packagemakesqr.jar
.On Windows, type:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrSample1.javaOn UNIX®, type:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./makesqr.jar makesqrSample1.javaReplace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may bematlabroot
C:\Program Files\MATLAB\R2025a
.Note
If
makesqr.jar
ormakesqrSample1.java
is not in the current directory, specify the full or relative path in the command. If the path contains spaces, surround it with double quotes.
Run the application using
java
.On Windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrSample1On UNIX, type:
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./makesqr.jar makesqrSample1Note
The dot (
.
) in the first position of the class path represents the current working directory. If it is not there, you get a message stating that Java cannot load the class.
The application returns the same output as the sample MATLAB script.
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
See Also
compiler.build.javaPackage
| mcc