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 generate sample Java code.

Prerequisites

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.

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

    Compiler tab with the Library Compiler app open

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

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

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

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

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

  2. Verify that the function defined in makesqr.m is mapped into Class1.

    Class Name section is defined as Class1

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

    See Specify Files to Install with Application.

Library Compiler app open with Samples section expanded

Package the Application

When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.

  1. Click Package.

    In the Save Project dialog box, specify the location to save the project.

  2. 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, and for_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.

  1. Save the path to the makesqr.m file located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp.

    appFile = fullfile(matlabroot,'toolbox','javabuilder','Examples', ...
        'MagicSquareExample','MagicDemoComp','makesqr.m');

  2. Save the following code in a sample file named makesqrSample1.m:

    x = 5;
    y = makesqr(x);

  3. 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');

    The compiler.build.Results object buildResults contains information on the build type, generated files, and build options.

    The function generates the following 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 file makesqrSample1.java.

    • GettingStarted.html — File that contains information on integrating your 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.

    You can specify additional options in the compiler.build command by using one or more of the following name-value arguments:

    • 'AdditionalFiles' — Path to additional files and folders to include in the package.

    • 'AutoDetectDataFiles' — Flag to automatically include data files.

    • 'ClassName'— Name of the Java class.

    • 'DebugBuild' — Flag to enable debug symbols.

    • 'OutputDir' — Path to the output directory that contains generated files.

    • 'PackageName'— Name of the generated package.

    • 'SampleGenerationFiles'— MATLAB sample files used to generate sample Java package files. For more information, see Sample Driver File Creation.

    • 'Verbose'— Flag to display progress information indicating compiler output during the build process.

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 Java application.

  1. Copy and paste the generated Java file makesqrSample1.java from the samples folder into the folder that contains the makesqr.jar package. If you used the Library Compiler, makesqr.jar is located in the for_testing folder.

  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\R2021b.

      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:

      javac -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 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

| | |

Related Topics