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 generate sample Java code.
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 Install and Configure 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, open
makesqr.m
located in
.matlabroot
\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp
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 Library Compiler App
Compile the function into a Java package using the Library Compiler app. Alternatively, if you want to create a Java package from the MATLAB command window using a programmatic approach, see Create Java Package Using compiler.build.javaPackage.
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler.
Alternatively, you can open the Library Compiler app from the MATLAB command prompt by entering:
libraryCompiler
In the Type section of the toolstrip, click Java Package.
In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.
In the Exported Functions section of the toolstrip, click .
In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.
The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.
For this example, select the file
makesqr.m
.In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:
Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the file name of the installer.
Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.
Note
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
Specify Package Settings
Next, define the name of your Java package and verify the class mapping for the .m
file
that you are building into your application.
Choose a name for your package. The Library Name field is automatically populated with
makesqr
as the name of the package. The same name is followed through in the package implementation steps below.Verify that the function defined in
makesqr.m
is mapped intoClass1
.
Create Sample Driver File
You can use any MATLAB file in the project to generate sample Java driver files. Although Java driver files are not necessary to create a package, you can use them to implement a Java application, as shown in Compile and Run MATLAB Generated Java Application.
In the Samples section, select Create New
Sample, and click makesqr.m
. A MATLAB file opens for you to edit.
% Sample script to demonstrate execution of function y = makesqr(x) x = 0; % Initialize x here y = makesqr(x);
Change x = 0
to x = 5
, save the file, and
return to the Library Compiler app.
Caution
You must edit the MATLAB sample file to output your desired result. Generated target language sample files use the same inputs and outputs as the sample MATLAB file.
The compiler converts this MATLAB code to Java code during packaging. For more information and limitations, see Sample Driver File Creation.
Customize the Application and Its Appearance
In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.
Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.
Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.
Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.
Files installed for your end user — Files that are installed with your application.
Package the Application
When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.
Click Package.
In the Save Project dialog box, specify the location to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the packaging process is complete, examine the generated output in the target folder.
Three folders are generated:
for_redistribution
,for_redistribution_files_only
, andfor_testing
.For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
The log file
PackagingLog.html
contains packaging results.
Create Java Package Using compiler.build.javaPackage
As an alternative to the Library Compiler app, you can create a Java package using a programmatic approach. If you have already created a package using the Library Compiler, see Compile and Run MATLAB Generated Java Application.
Save the path to the
makesqr.m
file located in
.matlabroot
\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoCompappFile = fullfile(matlabroot,'toolbox','javabuilder','Examples', ... 'MagicSquareExample','MagicDemoComp','makesqr.m');
Save the following code in a sample file named
makesqrSample1.m
:x = 5; y = makesqr(x);
Build the Java package using the
compiler.build.javaPackage
function. Use name-value arguments to add a sample file and enable verbose output.buildResults = compiler.build.javaPackage(appFile, ... '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 current working directory: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. If you used the Library Compiler,makesqr.jar
is located in thefor_testing
folder.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.bat365.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\R2023b
.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 code.
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
Library Compiler | compiler.build.javaPackage
| mcc
| deploytool