Create a Java Application with 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 Java package.

    1. Open makesqr.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 app.

    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.

  4. Specify the MATLAB functions you want to deploy.

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

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

      makesqr.m is located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\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.

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

  7. Click Package.

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

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

  10. Click Close on the Package window.

  11. Open the for_redistribution folder.

  12. Run the installer.

  13. In the folder containing the generated JAR files, create a new file called getmagic.java.

  14. Using a text editor, open getmagic.java.

  15. Paste the following code into the file.

    import com.mathworks.toolbox.javabuilder.*;
    import makesqr.*;
    
    class getmagic
    {
       public static void main(String[] args)
       {
          MWNumericArray n = null;
          Object[] result = null;
          Class1 theMagic = null;
    
          if (args.length == 0)
          {
            System.out.println("Error: must input a positive integer");
            return;
          }
    
          try
          {
             n = new MWNumericArray(Double.valueOf(args[0]),
                                          MWClassID.DOUBLE);
    
             theMagic = new Class1();
    
             result = theMagic.makesqr(1, n);
             System.out.println(result[0]);
          }
          catch (Exception e)
          {
             System.out.println("Exception: " + e.toString());
          }
          finally
          {
             MWArray.disposeArray(n);
             MWArray.disposeArray(result);
             theMagic.dispose();
          }
       }
    }
    
  16. Compile the Java application using javac.

    javac -classpath "mcrroot\toolbox\javabuilder\jar\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 where the MATLAB Runtime is installed on your system. If you have MATLAB installed on your system instead, you can use the path to your MATLAB installation.

  17. From the system's command prompt, run the application.

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

    You must be sure to place a dot (.) in the first position of the class path. If it not, 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 (;).

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

Was this topic helpful?