Main Content

我ntegrate SimpleMATLABFunction IntoJavaApplication

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

Files

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

Procedure

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

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

    At the MATLAB command prompt, navigate to the newMagicSquareExample\MagicDemoCompsubfolder in your work folder.

  2. Examine themakesqr.mfunction.

    functiony = makesqr(x) y = magic(x);

    At the MATLAB command prompt, entermakesqr (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 encapsulatesmakesqr.mby using theLibrary Compilerapp orcompiler.build.javaPackage.

    Use the following information for your project:

    包名 magicsquare
    Class Name magic
    File to Compile makesqr.m

    For example, if you are usingcompiler.build.javaPackage, type:

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

    For more details, see the instructions inGenerate 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 inMagicSquareExample\MagicDemoJavaApp\getmagic.java.

    getmagic.java

    The program does the following:

    • Creates anMWNumericArrayarray to store the input data

    • 我nstantiates amagicobject

    • Calls themakesqrmethod, 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 atry-catchblock to handle exceptions

    • Frees native resources usingMWArraymethods

  5. 我n MATLAB, navigate to theMagicDemoJavaAppfolder.

  6. Copy the generatedmagicsquare.jarpackage into this folder.

    • 我f you usedcompiler.build.javaPackage, type:

      copyfile(fullfile('..','MagicDemoComp','magicsquarejavaPackage','magicsquare.jar'))
    • 我f you used the Library Compiler, type:

      copyfile(fullfile('..','MagicDemoComp','magicsquare','for_testing','magicsquare.jar'))
  7. 我n a system command window, navigate to thePlotDemoJavaAppfolder.

  8. Compile the Java application usingjavac.

    • 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

    Replacematlabrootwith the path to your MATLAB orMATLAB Runtimeinstallation folder. For example, on Windows, the path may beC:\Program Files\MATLAB\R2022a.

    For more details, seeCompile 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";.\magicsquare.jar getmagic 5
    • On UNIX, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./magicsquare.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 usingcompiler.package.installer.

    • Try integrating a package that consists of multiple functions.

See Also

||||

Related Topics