Main Content

Pass Java Objects to MATLAB


This example shows you how to create a Java® application that finds a local minimum of an objective function using the MATLAB® optimization function fminsearch and the MWJavaObjectRef class.

In this example, you perform the following steps:

  1. Use MATLAB Compiler SDK™ to create a package that applies MATLAB optimization routines to objective functions implemented as Java objects.

  2. Access the MATLAB functions in a Java application, including use of the MWJavaObjectRef class to create a reference to a Java object and pass it to the generated Java methods.

  3. Build and run the application.

OptimDemo Package

The OptimDemo package finds a local minimum of an objective function and returns the minimal location and value.

The package uses the MATLAB optimization function fminsearch, and this example optimizes the Rosenbrock banana function used in the MATLAB fminsearch documentation.

The Optimizer class performs an unconstrained nonlinear optimization on an objective function implemented as a Java object. A method of this class, doOptim, accepts an initial guess and Java object that implements the objective function, and returns the location and value of a local minimum. The second method, displayObj, is a debugging tool that lists the characteristics of a Java object.

The two methods, doOptim and displayObj, encapsulate MATLAB functions.


MATLAB FunctionsdoOptim.m
MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\ObjectRefExample\ObjectRefDemoComp
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\ObjectRefExample\ObjectRefDemoJavaApp


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


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

  2. Examine the MATLAB code you want to access from Java. This example uses doOptim.m and displayObj.m.

    function [x,fval] = doOptim(h, x0)
    directEval = h.evaluateFunction(x0)
    wrapperEval = mWrapper(x0)
    [x,fval] = fminsearch(mWrapper,x0)
    function className = displayObj(h)
    className = class(h)

  3. Build the Java package with the Library Compiler app or using the following information:

    Library NameOptimDemo
    Class NameOptimizer
    Files to CompiledoOptim.m

    For example, if you are using, type:

    buildResults =["doOptim.m","displayObj.m"], ...
    'PackageName','OptimDemo', ...

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

  4. Write source code for a class that implements an object function to optimize. The code for this example is in the file

    The class implements the Rosenbrock banana function described in the MATLAB fminsearch documentation.

  5. Write source code for an application that accesses the MATLAB functions. The code for this example is in the file

    The program does the following:

    • Instantiates an object of the BananaFunction class above to be optimized.

    • Creates an MWJavaObjectRef that references the BananaFunction object, as shown:

      origRef = new MWJavaObjectRef(objectiveFunction);

    • Instantiates an Optimizer object.

    • Calls the displayObj method to verify that the Java object is being passed correctly.

    • Calls the doOptim method, which uses fminsearch to find a local minimum of the objective function.

    • Uses a try/catch block to handle exceptions.

    • Frees native resources using MWArray methods.

  6. In MATLAB, navigate to the ObjectRefDemoJavaApp folder.

  7. Copy the generated OptimDemo.jar package into this folder.

    • If you used, type:

    • If you used the Library Compiler, type:

  8. Open a command prompt window and navigate to the ObjectRefDemoJavaApp folder where you copied OptimDemo.jar.

  9. Compile the application and helper class using javac.

    • Windows®

      To compile, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar
      To compile, type:
      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar

    • UNIX®

      To compile, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./OptimDemo.jar
      To compile, type:
      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./OptimDemo.jar

    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\R2024a.

  10. Run the PerformOptim application.

    On Windows, type:

    java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar PerformOptim -1.2 1.0

    On Linux®, type:

    java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":.\OptimDemo.jar PerformOptim -1.2 1.0


    If you are running the application on the Mac 64-bit platform, you must add the -d64 flag in the Java command.

The PerformOptim program displays the following output:

Using x0 =
-1.2000    1.0000
** Properties of Java object                       **
h =
className =
  Name      Size            Bytes  Class             Attributes
  h         1x1                    BananaFunction              
Methods for class BananaFunction:
 BananaFunction    getClass          notifyAll         
equals            hashCode          toString          
evaluateFunction  notify            wait              
** Finished DISPLAYOBJ ******************************
** Performing unconstrained nonlinear optimization **
directEval =
 wrapperEval =
 x =
    1.0000    1.0000
 fval =
Optimization successful
** Finished DOOPTIM *********************************
Location of minimum: 
1.0000    1.0000
Function value at minimum: 

See Also


Related Topics