generate java package and build java application -凯发k8网页登录

main content

generate java package and build java application

supported platforms: windows®, linux®, mac

this example shows how to create a java® package from a matlab® function and generate sample java code.

prerequisites

  • verify that you have a version of java installed that is compatible with matlab compiler sdk™. for information on supported java versions, see .

    for information on configuring your development environment after installation, see .

  • end users must have an installation of matlab runtime to run the application. for details, see install and configure matlab runtime.

    for testing purposes, you can use an installation of matlab instead of matlab runtime.

create function in matlab

in matlab, examine the matlab code that you want to package. for this example, open makesqr.m located in matlabroot\toolbox\javabuilder\examples\magicsquareexample\magicdemocomp.

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

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

create java package using library compiler app

compile the function into a java package using the library compiler app. alternatively, if you want to create a java package from the matlab command window using a programmatic approach, see create java package using compiler.build.javapackage.

  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

    compiler tab with the library compiler app open

  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 add exported function to the project.

    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, select the file 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 file name of the installer.

    • runtime included in package — generate an application that includes the matlab runtime installer. you can specify the file name of the installer.

      note

      the first time you select this option, you are prompted to download the matlab runtime installer.

specify package settings

next, define the name of your java package and verify the class mapping for the .m file that you are building into your application.

  1. choose a name for your package. the library name field is automatically populated with makesqr as the name of the package. the same name is followed through in the package implementation steps below.

  2. verify that the function defined in makesqr.m is mapped into class1.

    class name section is defined as class1

create sample driver file

you can use any matlab file in the project to generate sample java driver files. although java driver files are not necessary to create a package, you can use them to implement a java application, as shown in compile and run matlab generated java application.

in the samples section, select create new sample, and click makesqr.m. a matlab file opens for you to edit.

% sample script to demonstrate execution of function y = makesqr(x)
x = 0; % initialize x here
y = makesqr(x);

change x = 0 to x = 5, save the file, and return to the library compiler app.

caution

you must edit the matlab sample file to output your desired result. generated target language sample files use the same inputs and outputs as the sample matlab file.

the compiler converts this matlab code to java code during packaging. for more information and limitations, see .

customize the application and its appearance

in the library compiler app, you can customize the installer, customize your application, and add more information about the application.

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

  • additional installer options — default installation path for the generated installer and custom logo selection. see .

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

  • files installed for your end user — files that are installed with your application.

    see .

library compiler app open with samples section expanded

package the application

when you are finished selecting your packaging options, save your library compiler project and generate the packaged application.

  1. 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 in the target folder.

    • three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      for more information about the files generated in these folders, see .

    • the log file packaginglog.html contains packaging results.

create java package using compiler.build.javapackage

as an alternative to the library compiler app, you can create a java package using a programmatic approach. if you have already created a package using the library compiler, see compile and run matlab generated java application.

  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;
    y = makesqr(x);
  3. build the java package using the compiler.build.javapackage function. use name-value arguments to add a sample file and enable verbose output.

    buildresults = compiler.build.javapackage(appfile, ...
    'samplegenerationfiles','makesqrsample1.m', ...
    'verbose','on');

    you can specify additional options in the compiler.build command by using name-value arguments. for details, see .

    the compiler.build.results object buildresults contains information on the build type, generated files, included support packages, and build options.

    the function generates the following files and folders 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.

    • includedsupportpackages.txt — text file that lists all support files included in the 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 .

    • 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. to create an installer using the buildresults object, see .

compile and run matlab generated java application

after creating your java package, you can call it from a java application. this example uses the sample java code generated during packaging. you can use this sample java application code as a guide to write your own application.

  1. copy and paste the generated java file makesqrsample1.java from the samples folder into the folder that contains the makesqr.jar package. if you used the library compiler, makesqr.jar is located in the for_testing folder.

  2. at the system command prompt, navigate to the folder that contains makesqrsample1.java and makesqr.jar.

  3. compile the application using javac. in the classpath argument, you specify the paths to javabuilder.jar, which contains the com.mathworks.toolbox.javabuilder package, and your generated java package makesqr.jar.

    • on windows, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrsample1.java
    • on unix®, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./makesqr.jar makesqrsample1.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\r2023a.

      note

      if makesqr.jar or makesqrsample1.java is not in the current directory, specify the full or relative path in the command. if the path contains spaces, surround it with double quotes.

  4. run the application using java.

    • on windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\makesqr.jar makesqrsample1
    • on unix, type:

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

      note

      the dot (.) in the first position of the class path represents the current working directory. if it is not there, you get a message stating that java cannot load the class.

    the application returns the same output as the sample matlab code.

        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

see also

| | |

related topics

网站地图