Main Content

Integrate Simple MATLAB Function Into Java Application

This example shows how to invoke a MATLAB® method that generates a magic square in a Java® application.

Files

MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp\makesqr.m
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoJavaApp\getmagic.java

Procedure

  1. Copy the MagicSquareExample folder that ships with MATLAB to your work folder:

    copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','MagicSquareExample'))

    At the MATLAB command prompt, navigate to the new MagicSquareExample\MagicDemoComp subfolder in your work folder.

  2. Examine the makesqr.m function.

    function y = makesqr(x)
    y = magic(x);
    disp(y);

    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
  3. Create a Java package that encapsulates makesqr.m by using the Library Compiler app or compiler.build.javaPackage.

    Use the following information for your project:

    Package Namemagicsquare
    Class Namemagic
    File to Compilemakesqr.m

    For example, if you are using compiler.build.javaPackage, type:

    buildResults = compiler.build.javaPackage('makesqr.m', ...
        'PackageName','magicsquare', ...
        'ClassName','magic');

    For more details, see the instructions in Generate Java Package and Build Java Application.

  4. Write source code for a Java application that accesses the MATLAB function.

    The sample application for this example is in MagicSquareExample\MagicDemoJavaApp\getmagic.java.

     getmagic.java

    The program does the following:

    • Creates an MWNumericArray array to store the input data

    • Instantiates a magic object

    • Calls the makesqr method, where the first parameter specifies the number of output arguments and the following parameters are passed to the function in order as input arguments

    • Uses a try-catch block to handle exceptions

    • Frees native resources using MWArray methods

  5. In MATLAB, navigate to the MagicDemoJavaApp folder.

  6. Copy the generated magicsquare.jar package into this folder.

    • If you used compiler.build.javaPackage, type:

      copyfile(fullfile('..','MagicDemoComp','magicsquarejavaPackage','magisquare.jar'))
    • If you used the Library Compiler, type:

      copyfile(fullfile('..','MagicDemoComp','magicsquare','for_testing','magicsquare.jar'))
  7. In a system command window, navigate to the PlotDemoJavaApp folder.

  8. Compile the Java application using javac.

    • On Windows®, execute this command:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\magicsquare.jar getmagic.java
    • On UNIX®, execute this command:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.jar getmagic.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.

    For more details, see Compile and Run MATLAB Generated Java Application.

  9. From the system command prompt, run the application.

    • On Windows, type:

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

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

    The application outputs a 5-by-5 magic square in the command window.

        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
  10. To follow up on this example:

    • Try running the generated application on a different computer.

    • Try building an installer for the package using compiler.package.installer.

    • Try integrating a package that consists of multiple functions.

See Also

| | | |

Related Topics