This example shows how to invoke a MATLAB® generated method in a Java® application.
To create a Java application that calls a MATLAB generated method:
Install the MATLAB Runtime and generated JAR files in one of the following ways:
Run the installer generated by MATLAB. It is
located in the for_redistribution
folder of
the deployment project.
Doing so automatically installs the MATLAB Runtime from the web and places the generated JAR files onto your computer.
Manually install the MATLAB Runtime and the generated JAR files onto your development system.
You can download the MATLAB Runtime installer from http://www.mathworks.com/products/compiler/mcr.
The generated JAR files are located in the MATLAB deployment
project's for_testing
folder.
In the folder containing the generated JAR files, create
a new file called getmagic.java
.
Using a text editor, open getmagic.java
.
Place the following as the first line in the file.
import com.mathworks.toolbox.javabuilder.*;
This statement imports the MATLAB support classes.
Place the following line after the first import statement.
import makesqr.*;
This statement imports the classes generated by the compiler.
Add the following class definition.
class getmagic { }
This class has a single main method that calls the generated class.
Add the
method
to the application.main()
public static void main(String[] args) { }
Add the following code to the top of the main()
method.
MWNumericArray n = null; Object[] result = null; Class1 theMagic = null;
This initializes the variables used by the application.
n is an instance of the MATLAB MWNumericArray
class
that MATLAB uses for its internal data format.
result is a generic Java object that holds the results of the call to MATLAB.
theMagic is an instance class generated from the MATLAB function.
Add the following code after the variable initialization.
if (args.length == 0) { System.out.println("Error: must input a positive integer"); return; }
This is a simple check to ensure that the required command-line argument was passed to the application.
Add a try/catch/finally block after the argument check.
In the try
section of the try/catch/finally
block, add the following code.
n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE);
The code instantiates an instance of MWNumericArray
and
populates it with a 1-by-1 array containing the integer passed to
the application on the command line. The value is converted to a Double
because
that is the most direct mapping between the Java and MATLAB internal
data representation.
After the code instantiating the input parameter, add the following to instantiate the class generated from MATLAB.
theMagic = new Class1();
The constructor for the generated class handles all of the setup required to start the MATLAB Runtime and populate it with the required MATLAB code.
Using the newly instantiated object, call the MATLAB function.
result = theMagic.makesqr(1, n); System.out.println(result[0]);
Add the following catch
section to
the try/catch/finally block to handle any exceptions that might be
thrown.
catch (Exception e) { System.out.println("Exception: " + e.toString()); }
Add the following finally
section to
the try/catch/finally block to clean up any resources.
finally { MWArray.disposeArray(n); MWArray.disposeArray(result); theMagic.dispose(); }
The disposeArray()
and dispose()
methods
clean up the resources used by the generated MATLAB code.
Save the Java file.
The completed Java file should resemble the following.
import com.mathworks.toolbox.javabuilder.*; import makesqr.*; class getmagic { public static void main(String[] args) { MWNumericArray n = null; Object[] result = null; Class1 theMagic = null; if (args.length == 0) { System.out.println("Error: must input a positive integer"); return; } try { n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE); theMagic = new Class1(); result = theMagic.makesqr(1, n); System.out.println(result[0]); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { MWArray.disposeArray(n); MWArray.disposeArray(result); theMagic.dispose(); } } }
Use the system's command line to navigate to the folder where you installed the generated Java package and saved the new Java file.
Compile the Java application using javac
.
javac -classpath "mcrroot\toolbox\javabuilder\jar\javabuilder.jar"; .\makesqr.jar .\getmagic.java
Note:
Enter the On UNIX® platforms, use colon ( |
mcrroot
is the path to where the MATLAB Runtime is
installed on your system. If you have MATLAB installed on your
system instead, you can use the path to your MATLAB installation.
From the system's command prompt, run the application.
java -classpath .;"mcrroot\toolbox\javabuilder\jar\javabuilder.jar"; .\makesqr.jar getmagic 5 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
You must be sure to place a dot (.
) in the
first position of the class path. If it not, you get a message stating
that Java cannot load the class.
Note:
Enter the On UNIX platforms, use colon ( |
mcrroot
is the path to where the MATLAB Runtime is
installed on your system. If you have MATLAB installed on your
system instead, you can use the path to your MATLAB installation.
To follow up on this example:
Try installing the new application on a different computer.
Try building an installer for the application.
Try integrating a package that consists of multiple functions.