Main Content

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

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.

  1. 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.

  2. Build the Java package using the compiler.build.javaPackage function and the makesqr.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, see compiler.build.javaPackage.

    The compiler.build.Results object buildResults 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 file makesqrSample1.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, see compiler.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.

  1. Copy and paste the generated Java file makesqrSample1.java from the samples folder into the folder that contains the makesqr.jar package.

  2. At the system command prompt, navigate to the folder that contains makesqrSample1.java and makesqr.jar.

  3. Compile the application using javac. In the classpath argument, you specify the paths to javabuilder.jar, which contains the com.mathworks.toolbox.javabuilder package, and your generated Java package makesqr.jar.

    • On Windows, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrSample1.java
    • On UNIX®, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./makesqr.jar makesqrSample1.java

      Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may be C:\Program Files\MATLAB\R2025a.

      Note

      If makesqr.jar or makesqrSample1.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.

  4. Run the application using java.

    • On Windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrSample1
    • On UNIX, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./makesqr.jar makesqrSample1

      Note

      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

|

Topics