Create a Java Package from MATLAB Code

This example shows how to create a Java® package using a MATLAB® function. You can then pass the generated package to the developer, who is responsible for integrating it into an application.

To compile a Java package from MATLAB code:

  1. In MATLAB, examine the MATLAB code that want to deploy as a shared library.

    1. Open makesqr.mmakesqr.m.

      function y = makesqr(x)
      y = magic(x);
    2. At the MATLAB command prompt, enter makesqr(5).

      The output appears as follows:

      ans =
          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
  2. Open the Library Compiler.

    1. On the toolstrip, select the Apps tab.

    2. Click the arrow at the far right of the tab to open the apps gallery.

    3. Click Library Compiler.

  3. In the Application Type section of the toolstrip, select Java Package from the list.

      Note:   If the Application Type section of the toolstrip is collapsed, you can expand it by clicking the down arrow.

  4. Specify the MATLAB functions you want to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus button.

        Note:   If the Exported Functions section of the toolstrip is collapsed, you can expand it by clicking the down arrow.

    2. In the file explorer that opens, locate and select the makesqr.m file.

      makesqr.m is located in matlabroot\toolbox\javabuilder\Examples\MagicSqaureExample\MagicDemoComp.

    3. Click Open to select the file, and close the file explorer.

      makesqr.m is added to the list of exported files and a minus button appears under the plus button. In addition, makesqr is set as:

      • the library name

      • the package name

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

  6. In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.

      Note:   If the Packaging Options section of the toolstrip is collapsed, you can expand it by clicking the down arrow.

    This option creates an application installer that automatically downloads the MATLAB runtime and installs it along with the deployed package.

  7. Explore the main body of the MATLAB Compiler project window.

    The project window is divided into the following areas:

    • Application Information — Editable information about the deployed application. This information is used by the generated installer to populate the installed application's metadata. See Customize the Installer.

    • Class mapper — Description of how the MATLAB functions are mapped to Java classes.

    • Additional Installer Options — Default installation path for the generated installer. See Customize the Installer.

    • Files required for your application — Additional files required by the generated application. These files will be included in the generated application installer. See Manage Required Files in a Compiler Project.

    • Files installed with your application — Files that are installed with your application. These files include:

      • readme.txt

      • .jar file

      • doc folder

      See Specify Files to Install with the Application.

  8. Click Package.

  9. Select the Open output folder when process completes check box.

  10. Verify that the generated output contains:

    • for_redistribution — A folder containing the installer to distribute the package

    • for_testing — A folder containing the raw generated files to create the installer

    • for_redistribution_files_only — A folder containing only the files needed to redistribute the package

    • PackagingLog.txt — A log file generated by the compiler

  11. Click Close on the Package window.

To follow up on this example:

  • Try running the function from the command line as follows:

    1. Open a system command prompt.

    2. Navigate to the for_testing folder of the generated deployment project.

    3. Enter the following command:

      java -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";makesqr.jar makesqr.Class1 5

      matlabroot is the full path to your MATLAB installation.

  • Try creating a package that consists of more than one function.

  • Try Integrate a Java Package into an Application

Was this topic helpful?