Main Content

Generate a Java Package and Build a Java Application

Supported platforms: Windows®, Linux®, Mac

This example shows how to create a Java® package that uses a MATLAB® function. You can integrate the generated package into a Java application. This example also shows how to call the Java package from a Java application. The target system does not require a licensed copy of MATLAB.

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want packaged. For this example, open makesqr.m located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp.

 makesqr.m

At the MATLAB command prompt, enter makesqr(5).

The output is:

    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 Application Using Library Compiler App

  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

  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 .

    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, navigate to matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp and select 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 filename of the installer.

    • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the filename of the installer.

      Note

      The first time you select this option, you are prompted to download the MATLAB Runtime installer.

Specify Package Settings

  1. The Library Name field is automatically populated with makesqr as the name of the package. The same name is followed through in the implementation of the package.

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

  3. Add MATLAB files to generate the sample Java driver files. Although Java driver files are not necessary to create packages, they are used to demonstrate how to Install and Implement MATLAB Generated Java Application.

    In the Samples section, select Create New Sample, and click makesqr.m. A MATLAB file opens for you to edit. Define the input variables as necessary for your application, save the file, and return to the Library Compiler app. For more information and limitations, see Sample Driver File Creation.

Customize the Application and Its Appearance

You can customize the installer, customize your application, and add more information about the application as follows:

  • 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

Package the Application

  1. To generate the packaged application, 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.

    • Three folders are generated in the target folder location: 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.

    • PackagingLog.txt — Log file generated by MATLAB Compiler™.

Create Java Package Using compiler.build.javaPackage

Note

If you have already created a Java package using the Library Compiler app, you can skip this section. However, if you want to know how to create a Java package from the MATLAB command window using a programmatic approach, follow these instructions.

  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;
    a = makesqr(x);

  3. Build the Java package using the compiler.build.javaPackage function and the makesqr.m file. Use name-value arguments to add a sample file and enable verbose output.

    buildResults = compiler.build.javaPackage('makesqr.m',...
    'SampleGenerationFiles','makesqrSample1.m',...
    'Verbose','on');

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

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

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

Install and Implement MATLAB Generated Java Application

After creating your Java packages, you can call them from a Java application. The Java application that you create uses the sample Java driver code generated during packaging. The Java driver code calls the Java packages, and it is based on the sample MATLAB file you selected in previous setup steps.

These steps are also explained in the GettingStarted.html file. Before starting, make sure that you Install and Configure MATLAB Runtime, and that you have the Java Development Kit installed.

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

  2. Use the system command line to navigate to the folder that contains the generated sample Java driver code file and the makesqr.jar file.

  3. Compile the application using javac at the system command prompt.

    javac -classpath "mcrroot\toolbox\javabuilder\jar\platform\javabuilder.jar";.\makesqr.jar .\getmagic.java

    Note

    On UNIX® platforms, use colon (:) as the class path delimiter instead of semicolon (;).

    mcrroot is the path to the MATLAB Runtime installation on your system. If you have MATLAB installed on your system instead, you can use the path to your MATLAB installation.

  4. From the system command prompt, run the application. If you used sample MATLAB code in the packaging steps, this application should return the same output as the sample code.

    java -classpath .;"mcrroot\toolbox\javabuilder\jar\platform\javabuilder.jar";.\makesqr.jar makesqrSample1
        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

    Place a dot (.) in the first position of the class path to represent the current working directory. If it is not there, you get a message stating that Java cannot load the class.

    Note

    On UNIX platforms, use colon (:) as the class path delimiter instead of semicolon (;).

See Also

| | |