Main Content

Map Functions to Java Classes

Map Functions to Java Classes using compiler.build.javaPackage

The compiler.build.javaPackage function accepts a class map argument for mapping MATLAB® functions to Java® classes.

For example, create a containers.Map object whose keys are class names and whose values are the locations of function files. The functions exampleFcn1.m and exampleFcn2.m are mapped to Class1 and exampleFcn3.m and exampleFcn4.m are mapped to Class2.

cmap = containers.Map;
cmap('Class1') = {'exampleFcn1.m','exampleFcn2.m'};
cmap('Class2') = {'exampleFcn3.m','exampleFcn4.m'};

Then, build the Java package using compiler.build.javaPackage and cmap.

compiler.build.javaPackage(cmap);

Map Functions to Java Classes with mcc

When using mcc to generate Java applications, you map your MATLAB functions into Java classes based on the list into which they are placed on the command line. Class groupings are specified by adding one or more class{className:mfilename...} entries to the command line. All of the files not included in a class grouping are added to the class specified by the -W java:packageName,className flag.

For example, mcc —W java:myPackage,MyClass fun1.m fun2.m fun3.m generates a Java application myPackage that contains a single class MyClass. MyClass has three methods: fun1, fun2, and fun3.

However, mcc —W java:myPackage,MyClass fun1.m fun2.m class{MyOtherClass:fun3.m} generates a Java application myPackage that contains two classes: MyClass and MyOtherClass. MyClass has two methods: fun1 and fun2. MyOtherClass has one method fun3.