1. the MATLAB version must support Java
Run deploytool in command mode. If this command is supported, use
2. Function writing in MATLAB
% Defines a function operation (a, B), calculates the addition, subtraction, multiplication, division, and result of A and B.
% Function definition function output variable list [s, M,...] function name (input variable list) sum, sub, Mul, div
Function [Sum, sub, Mul, div] = operation (A, B );
Sum = A + B;
Sub = A-B;
Mul = A * B;
DIV = A/B;
End
PS: The red part above is related to the return value of calling the operation in Java. The object result [] = xxx called in Java. operation (4, a, B); 4 indicates that four return values are stored in result [0] to result [4.
3. Use MATLAB to compile and generate jar
Enter deploytool In the MATLAB command window and a compilation window will pop up,
Click file and select new-deployment project,
Then select MATLAB builder Jave.
Enter the project name,
Ocation: Enter the storage location of the project.
Click OK.
Then, add the M file to the project. First, create a new class. The class name is the name of the new class you will need in Java, that is, the Class Name of operation in MATLAB, and then add your M file to this class directory, and then build it directly.
4. Use MATLAB Functions in Java
Now there is a new Java project under eclipse, and XXX. jar andUnder your MATLAB installation directory/Toolbox/javabuilder/JAR/javabuilder. jar is added to your Java project extend Lib.
Then you can clearly see the functions and classes contained in XXX. jar.
[Java]View plaincopyprint?
- Import com. Mathworks. toolbox. javabuilder. mwexception;
- Import com. Mathworks. toolbox. javabuilder. mwnumericarray;
- Import XXX. xxxx;
- Class test {
- Publicstaticvoid main (string [] ARGs ){
- Try {
- XXXX abc = new xxxx ();
- Object [] result = NULL;/* stores the result */
- Int A = 10, B = 2;
- Result = ABC. Operation (4, A, B );
- System. Out. println (result [0]. tostring ());
- System. Out. println (result [1]. tostring ());
- System. Out. println (result [2]. tostring ());
- System. Out. println ();
- } Catch (mwexception E1 ){
- // Todo auto-generated Catch Block
- E1.printstacktrace ();
- } Finally {
- /* Free Native Resources */
- System. GC ();
- }
- }
If result [0] returns an array, you can solve this problem using the following method:
Mwnumericarray A = new mwnumericarray (double. valueof (result [0]. tostring (), mwclassid. Double );
Mwnumericarray temp = (mwnumericarray) result [0];
Float [] [] weights = (float [] []) temp. tofloatarray ();
5. Possible Problems
Exception in thread "Main" Java. lang. unsatisfiedlinkerror: failed to find the library libmwmclmcrrt. so.7.13, required by MATLAB builder Ja, on java. library. path. this library is typically installed along with MATLAB or the MCR, its absence may indicate an issue with that installation or the current path configuration. the MCR version that this component is trying to use is: 7.13.
Failed to find the library mclmcrrt710.dll, required by MATLAB builder Ja, on java. Library. Path
The reason is that 2009a needs to install mclmcrrt7.10. Find \ toolbox \ compiler \ deploy \ win32in the installation directory of Matlab, find mcrinstaller.exe to install it, and restart eclipse.
You may also encounter:
Exception in thread "Main" Java. Lang. unsatisfiedlinkerror: Can't Load Library: D: \ Program Files \ MATLAB \ r2009a \ bin \ Win32 \ nativemcl. dll
At java. Lang. classloader. loadlibrary (unknown source)
At java. Lang. runtime. load0 (unknown source)
At java. Lang. system. Load (unknown source)
At com. Mathworks. toolbox. javabuilder. Internal. mwmcr. <clinit> (mwmcr. Java: 1449)
At operation. operationmcrfactory. <clinit> (operationmcrfactory. Java: 99)
At operation. Operation. <init> (operation. Java: 61)
At javatestmatlab. Main (javatestmatlab. Java: 18)
The nativemcl. dll file cannot be found. My system is a 64-bit system, so the win64 folder is under the bin directory. To adapt, I created a Win32 folder under the bin directory and copied nativemcl. DLL to it.
The following error is reported:
Exception in thread "Main" Java. lang. unsatisfiedlinkerror: D: \ Program Files \ MATLAB \ r2009a \ bin \ Win32 \ nativemcl. DLL: Can't load amd 64-bit. DLL on a Ia 32-bit Platform
At java. Lang. classloader $ nativelibrary. Load (native method)
At java. Lang. classloader. loadlibrary0 (unknown source)
At java. Lang. classloader. loadlibrary (unknown source)
At java. Lang. runtime. load0 (unknown source)
At java. Lang. system. Load (unknown source)
At com. Mathworks. toolbox. javabuilder. Internal. mwmcr. <clinit> (mwmcr. Java: 1449)
At operation. operationmcrfactory. <clinit> (operationmcrfactory. Java: 99)
At operation. Operation. <init> (operation. Java: 61)
At javatestmatlab. Main (javatestmatlab. Java: 18)
When using eclipse for development, one jar package must use JNI to reference the DLL file. This error occurs.
- Can't load amd 64-bit. dll on a Ia 32-bit Platform
My system is win7 64-bit, I found that eclipse uses the JRE is a 32-bit solution to run using jre_x64, put the DLL file into the project below: http://www.oracle.com/technetwork/java/javase/downloads/jre-6u26-download-400751.html Configuration:
Select jre_x64 runtime when creating a project:
End