Create Java Package Using Java Package Compiler App
Supported platforms: Windows®, Linux®, Mac
This example shows how to use the Java Package Compiler app to package multiple MATLAB® functions into a Java® package. You can call the package in a Java application that computes a magic square.
Before R2025a: Create a Java package using the Library Compiler as shown in Generate Java Package and Build Java Application (R2024b).
Prerequisites
Verify that you have a version of Java installed that is compatible with MATLAB Compiler SDK™. For details, see MATLAB Supported Interfaces to Other Languages.
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.
Write Deployable MATLAB Code
In MATLAB, examine the MATLAB code that you want to package. This example uses these files in
.
\toolbox\javabuilder\Examples\MagicSquareExamplematlabroot
MATLAB Function | MagicSquareExample\MagicDemoComp\makesqr.m |
Java Application Code | MagicSquareExample\MagicDemoJavaApp\getmagic.java |
At the MATLAB command prompt, copy the contents of the
MagicSquareExample
folder that ships with MATLAB to a new folder named MagicProjectJava
.
copyfile(fullfile(matlabroot,"toolbox","javabuilder","Examples","MagicSquareExample"),"MagicProjectJava");
Examine the MATLAB function makesqr.m
located in the
MagicDemoComp
folder.
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 Project and Compiler Task
Create a compiler task for your function using the Java Package Compiler. Compiler tasks allow you to compile files in a project for a specific deployment target.
To open the app, on the Apps tab, expand the Apps gallery. In the Application Deployment section, click Java Package Compiler.
You can also open the app using the javaPackageCompiler
function at
the MATLAB Command Window.
After you open the app, the Create Compiler Task dialog box prompts you to add a
compiler task to a new or an existing MATLAB project. For this example, select Start a new project and create a
compiler task and create a new project named MagicProject
in your working folder. For more information on creating and using MATLAB projects, see Create Projects.
A new compiler task named JavaPackage1
opens in the Editor. You can
compile code for other deployment targets by opening the Compiler Task Manager
or going to the Manage Tasks tab and creating a new compiler
task.
Specify Build Options
You can specify options for the Java package and its installer before packaging to customize the building and packaging process. For instance, you can obfuscate the MATLAB code or specify the method of including MATLAB Runtime in the generated installer.
Add a MATLAB function to the Java package. All files must be located in the project root folder to be added to
the project. For this example, in the Exported Functions section of
the compiler task, click Add File and select
makesqr.m
. In the Project panel, the file now has the labels
Design
and Exported Function File
.
In the Package Info section, replace the string My Java
Package
with the name for your Java package, magicsquare
.
In the Build Settings section, replace the string
Class1
with the name for the Java class, magic
.
View Code and Package Java Package
To view code that contains instructions on building and packaging your component, click
the arrow next to the Export Build Script button and select
Show Code. On the right, a window displays a deployment script with
the compiler.build.javaPackage
and compiler.package.installer
functions that corresponds to your build options.
You can convert this code to a MATLAB script file by clicking the Export Build Script button. Running the generated build script is equivalent to clicking the Build and Package button.
To create the Java package, click Build and Package.
The compiler generates files in the
folder
in your project folder. The <compiler_task_name>
/outputbuild
subfolder contains the Java package, and the package
subfolder contains an installer
for your package along with MATLAB Runtime. To choose a different output location for the generated files, update the
paths in the Output Locations section.
Compile and Run Java Application
After creating the Java package, write source code for a Java application that calls the MATLAB functions in the package.
A Java application for this example named getmagic.java
is
located in the folder MagicDemoJavaApp
. Copy or move this file into the
folder that contains your generated package, magicsquare.jar
.
The getmagic
application performs these actions.
Creates an
MWNumericArray
array to store the input data.Instantiates a
magic
object.Calls the
makesqr
method, where the first parameter specifies the number of output arguments and the subsequent parameters are passed to the function in order as input arguments.Uses a
try
-catch
block to handle exceptions.Frees native resources using
MWArray
methods.
To test your application in MATLAB before deployment, navigate to the folder that contains both your generated
deployable code archive magicsquare.jar
and application
getmagic.java
. Then, run the following commands using the matlabroot
, fullfile
, and system
functions.
compileCommand = ['javac -cp magicsquare.jar;"' ... fullfile(matlabroot,'toolbox','javabuilder','jar', ... 'javabuilder.jar') '" getmagic.java']; runCommand = ['java -cp .;magicsquare.jar;"' ... fullfile(matlabroot,'toolbox','javabuilder','jar', ... 'javabuilder.jar') '" getmagic 5'] system([compileCommand ' && ' runCommand]);
Note
Modify the paths to getmagic.java
and
magicsquare.jar
in the above commands as needed.
To deploy the Java package, distribute the files magicsquare.jar
and
getmagic.java
to the end user.
To run a Java package generated by MATLAB Compiler SDK, you must install MATLAB Runtime. For details, see Download and Install MATLAB Runtime. If you created an installer using Build and Package, the installer contains a version of MATLAB Runtime that matches the version of MATLAB used to compile the Java package.
To compile and run the Java application outside of MATLAB, open a system command prompt window and navigate to the folder that contains
magicsquare.jar
and getmagic.java
.
Compile the Java application using javac
.
On Windows, enter this command.
javac -classpath "
<matlabroot>
\toolbox\javabuilder\jar\javabuilder.jar";magicsquare.jar getmagic.javaOn UNIX®, enter 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. For example, on Windows, the path may be <matlabroot>
C:\Program Files\MATLAB\MATLAB
Runtime\R2025a
.
From the system command prompt, run the application with the input argument
5
.
On Windows, enter this command.
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";magicsquare.jar getmagic 5On UNIX, enter this command.
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":magicsquare.jar getmagic 5
The application displays a 5-by-5 magic square matrix.
Magic square of order 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
To further explore this example:
Try running the generated application on a different computer.
Try building a Java package using
compiler.build.javaPackage
.Try integrating a package that consists of multiple functions. For an example, see Create Java Application with Multiple MATLAB Functions.
See Also
Java Package
Compiler | compiler.build.javaPackage
| compiler.package.installer