Pass Java Objects to MATLAB
Overview
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:
Use MATLAB Compiler SDK™ to create a package that applies MATLAB optimization routines to objective functions implemented as Java objects.
Access the MATLAB functions in a Java application, including use of the
MWJavaObjectRefclass to create a reference to a Java object and pass it to the generated Java methods.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.
Files
| MATLAB Functions | doOptim.m
displayObj.m
|
| MATLAB Function Location | |
| Java Code Location | |
javabuilder.jar | |
Procedure
Copy the
ObjectRefExamplefolder that ships with MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','ObjectRefExample'),'ObjectRefExample')
At the MATLAB command prompt, navigate to the new
ObjectRefExample\ObjectRefDemoCompsubfolder in your work folder.Examine the MATLAB code you want to access from Java. This example uses
doOptim.manddisplayObj.m.function [x,fval] = doOptim(h, x0) directEval = h.evaluateFunction(x0) wrapperEval = mWrapper(x0) [x,fval] = fminsearch(mWrapper,x0)function className = displayObj(h) h className = class(h) whos('h') methods(h)
Build the Java package with the Java Package Compiler app or
compiler.build.javaPackageusing the following information:Field Value Java Package Name OptimDemoClass Name OptimizerFiles to Compile doOptim.m
displayObj.mFor example, if you are using
compiler.build.javaPackage, type:buildResults = compiler.build.javaPackage(["doOptim.m","displayObj.m"], ... 'PackageName','OptimDemo', ... 'ClassName','Optimizer');
For more details, see the instructions in Generate Java Package and Build Java Application.
Write source code for a class that implements an object function to optimize. The code for this example is in the file
BananaFunction.java.The class implements the Rosenbrock banana function described in the MATLAB
fminsearchdocumentation.Write source code for an application that accesses the MATLAB functions. The code for this example is in the file
PerformOptim.java.The program does the following:
Instantiates an object of the
BananaFunctionclass above to be optimized.Creates an
MWJavaObjectRefthat references theBananaFunctionobject, as shown:.origRef = new MWJavaObjectRef(objectiveFunction);Instantiates an Optimizer object.
Calls the
displayObjmethod to verify that the Java object is being passed correctly.Calls the
doOptimmethod, which usesfminsearchto find a local minimum of the objective function.Uses a
try/catchblock to handle exceptions.Frees native resources using
MWArraymethods.
In MATLAB, navigate to the
ObjectRefDemoJavaAppfolder.Copy the generated
OptimDemo.jarpackage into this folder.For instance, if you used
compiler.build.javaPackage, type:copyfile(fullfile('..','ObjectRefDemoComp','OptimDemojavaPackage','OptimDemo.jar'))
Open a command prompt window and navigate to the
ObjectRefDemoJavaAppfolder where you copiedOptimDemo.jar.Compile the
PerformOptim.javaapplication andBananaFunction.javahelper class usingjavac.Windows®
To compile
BananaFunction.java, type:To compilejavac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar BananaFunction.javaPerformOptim.java, type:javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar PerformOptim.javaUNIX®
To compile
BananaFunction.java, type:To compilejavac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./OptimDemo.jar BananaFunction.javaPerformOptim.java, type:javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./OptimDemo.jar PerformOptim.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may bematlabrootC:\Program Files\MATLAB\R2025b.Run the
PerformOptimapplication.On Windows, type:
java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\OptimDemo.jar PerformOptim -1.2 1.0On Linux®, type:
java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":.\OptimDemo.jar PerformOptim -1.2 1.0Note
If you are running the application on the Mac 64-bit platform, you must add the
-d64flag in the Java command.
The PerformOptim program displays the following
output:
Using x0 =
-1.2000 1.0000
*****************************************************
** Properties of Java object **
*****************************************************
h =
BananaFunction@1766806
className =
BananaFunction
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 =
24.2000
wrapperEval =
24.2000
x =
1.0000 1.0000
fval =
8.1777e-10
Optimization successful
** Finished DOOPTIM *********************************
Location of minimum:
1.0000 1.0000
Function value at minimum:
8.1777e-10
See Also
compiler.build.javaPackage | Java Package
Compiler
