Main Content

Limitations on Multiple Packages in Single Java Application

When developing Java® applications that use multiple MATLAB® packages, consider that the following types of data cannot be shared between packages:

  • MATLAB function handles

  • MATLAB figure handles

  • MATLAB objects

  • C, Java, and .NET objects

  • Executable data stored in cell arrays and structures

To work around these limitations, you can combine multiple Java packages into a single package.

Combine Packages with MATLAB Function Handles

You can pass MATLAB function handles between an application and the MATLAB Runtime instance from which it originated. However, a MATLAB function handle cannot be passed into a MATLAB Runtime instance other than the one in which it originated. For example, suppose you had two MATLAB functions, get_plot_handle and plot_xy, and plot_xy uses the function handle created by get_plot_handle.

% Saved as get_plot_handle.m
function h = get_plot_handle(lnSpec, lnWidth, mkEdge, mkFace, mkSize)
h = @draw_plot;
    function draw_plot(x, y)
        plot(x, y, lnSpec, ...
            'LineWidth', lnWidth, ...
            'MarkerEdgeColor', mkEdge, ...
            'MarkerFaceColor', mkFace, ...
            'MarkerSize', mkSize)
    end
end
% Saved as plot_xy.m
function plot_xy(x, y, h)
h(x, y);
end

If you compile them into two Java packages, the call to plot_xy would throw an exception.

import com.mathworks.toolbox.javabuilder.*;
import get_plot_handle.*;
import plot_xy.*;

class plottter
{
   public static void main(String[] args)
   {
     MWArray h = null;

      try
      {
        plotter_handle = new get_plot_handle.Class1();
        plot = new plot_xy.Class1();

        h = plotter_handle.get_plot_handle(1,'--rs',2.0,'k','g',10);
        double[] x = {1,2,3,4,5,6,7,8,9};
        double[] y = {2,6,12,20,30,42,56,72,90};
        plot.plot_xy(x, y, h);
      }
      catch (Exception e)
      {
         System.out.println("Exception: " + e.toString());
      }
      finally
      {
         MWArray.disposeArray(h);
         plot.dispose();
         plotter_handle.dispose();
      }
   }
}

The way to correct the situation is to compile both functions into a single package.

import com.mathworks.toolbox.javabuilder.*;
import plot_functions.*;

class plotter
{
   public static void main(String[] args)
   {
     MWArray h = null;

      try
      {
        plot_funcs = new Class1();

        h = plot_funcs.get_plot_handle(1, '--rs', 2.0, 'k', 'g', 10);
        double[] x = {1,2,3,4,5,6,7,8,9};
        double[] y = {2,6,12,20,30,42,56,72,90};
        plot_funcs.plot_xy(x, y, h);
      }
      catch (Exception e)
      {
         System.out.println("Exception: " + e.toString());
      }
      finally
      {
         MWArray.disposeArray(h);
         plot_funcs.dispose();
      }
   }
}

You could also correct this situation by using a singleton MATLAB Runtime. For more information, see Share MATLAB Runtime Instances.

Combining Packages with Objects

MATLAB Compiler SDK™ enables you to return the following types of objects from MATLAB Runtime to your application code:

  • MATLAB

  • C++

  • .NET

  • Java

However, you cannot pass an object created in one MATLAB Runtime instance into a different MATLAB Runtime instance. This conflict can happen when a function that returns an object and a function that manipulates that object are compiled into different packages.

For example, you develop two functions. The first creates a bank account for a customer based on some set of conditions. The second transfers funds between two accounts.

% Saved as account.m
classdef account < handle

    properties
        name
    end
    
    properties (SetAccess = protected)
        balance = 0
        number
    end
    
    methods
        function obj = account(name)
            obj.name = name;
            obj.number = round(rand * 1000);
        end
        
        function deposit(obj, deposit)
            new_bal = obj.balance + deposit;
            obj.balance = new_bal;
        end
        
        function withdraw(obj, withdrawl)
            new_bal = obj.balance - withdrawl;
            obj.balance = new_bal;
        end
        
    end
end
% Saved as open_acct .m
function acct = open_acct(name, open_bal )

    acct = account(name);

    if open_bal > 0
        acct.deposit(open_bal);
    end
    
end
% Saved as transfer.m
function transfer(source, dest, amount)

    if (source.balance > amount)
        dest.deposit(amount);
        source.withdraw(amount);
    end

end

If you compiled open_acct.m and transfer.m into separate packages, you could not transfer funds using accounts created with open_acct. The call to transfer throws an exception. One way of resolving this conflict is to compile both functions into a single package. You could also refactor the application so that you are not passing MATLAB objects to the functions. You could also use a singleton MATLAB Runtime.For more information, see Share MATLAB Runtime Instances.

Related Topics