近期参与项目负责把Matlab编写代码打包成为Jar文件交付给Java项目使用,并对之间的衔接部分编写接口代码,遇到了很多问题,学习了Matlab CompilerSDK 的使用,现分享总结如下:
我的网盘分享(matlab2017a)
链接:https://pan.baidu.com/s/15a8xLdiPvxbrWpSJDaXmcA 密码:os6t一、Matlab Compiler SDK 简述
Matlab Compiler SDK支持将Matlab程序封装为C/C++、.NET、Java 或 Python的类库,从而实现对于Matlab程序的调用,除此之外Matlab Compiler对于Matlab代码进行了封装,保护了代码安全。
二、Matlab Compiler SDK 使用
以将Matlab程序封装为Java类库Jar文件为例,描述该功能使用的过程:
1.首先确认Matlab中使用的JDK与项目使用是否一致,因为Matlab打包成Jar时,需要调用Javac生成class文件,JDK与项目使用不同会导致无法调用。注意:本项目中使用的是JDK1.8,版本较高,在matlab2017a以前的版本均不支持此版本的jdk,在matlab2017a以后的版本可以使用此版本进行打包。
图1.获取matlab使用jdk
图2.获取本机项目使用jdk
function [Res] = add(x,y)
Res = x + y;
end
2.使用deploytool在命令行打开打包部署工具,使用Library Compiler进行类库类型的打包,各项功能如下图所示:
图3.打开部署工具
3.打开Matlab Compiler,选取打包类库语言,选择matlab程序,填写相关类名等信息。
图4.项目打包页面
图5.项目打包
图6.打包完成
生成文件解释:
· for_redistribution— Folder containing thefile that installs the application and the MATLAB Runtime.(MATLAB Runtime安装文件)
· for_testing— Folder containing all the artifacts createdby mcc,such as binaries and JAR, header, and source files for a specific target. Use thesefiles to test the installation. (生成的jar,jar中类文件源码,.class文件,以及相应的文档资料)
· for_redistribution_files_only— Folder containing thefiles required for redistributing the application. Distribute these files to userswho have MATLAB or MATLAB Runtime installed on their machines. (生成的jar以及相应的文档资料)
· PackagingLog.html— Log file generated byMATLAB Compiler™(打包的日志记录)
三、在IDE中调用Matlab打包生成的Jar
1. 配置安装matlab runtime环境(MCR),若在本机运行,由于安装了matlab则无需再重复安装,若一致到其他设备运行,需要安装matlab runtime(参照下文4.4)。
2. 从生成的文件夹中拷贝for_redistribution_files_only/addLib.jar以及拷贝matlab安装目录下\matlab2017a\toolbox\javabuilder\jar\javabuilder.jar,将其载IDE中的path中。
3. 编码调用matlab程序并使用dispose方法释放创建的实例(原因参照下文4.6)。
package cn.com.anshirui.getfeatures.test;
import com.mathworks.toolbox.javabuilder.MWException;
import addlib.Add;
/**
* @author guomz
* @date 2018年7月9日 下午5:20:37
* @version
@TODO matlab编码测试
*/
public class AddTest {
public static void main(String[] args) throws MWException {
int a = 1 , b = 2;
Add addInstance = new Add();
Object[] res = addInstance.add(a, b);
System.out.println(res[0]);
addInstance.dispose();
}
}
四、常见错误
1.在命令行输入depolytool命令后没有启动部署工具。
原因:安装matlab时未勾选此工具箱,参阅matlab工具箱安装步骤安装Matlab Compiler toolbox。
2.上文提到的,matlab版本与jdk版本不匹配的问题。
报错:
ERROR:An error occurredwhile shelling out to javadoc (Error code = 1). Unable to build executable
解决方案:
①更换matlab版本;②降低jdk版本;
3.由于中文字符存在导致的编码异常。
报错:
D:\zytc\704zhangwei\matlabpackage\GetFeaturesLib\for_testing\GetFeaturesLib\GetFeatures.java:630:错误: 格式错误的 HTML
* % 用来滤除不正常的RR间期,滤除方法为:若 RR1.3mean
D:\zytc\704zhangwei\matlabpackage\GetFeaturesLib\for_testing\GetFeaturesLib\GetFeatures.java:630:错误: '>' 的用法错误
* % 用来滤除不正常的RR间期,滤除方法为:若 RR1.3mean
Error: An error occurred whileshelling out to javadoc (error code = 1).Unable to build executable. For more information,pass the -v option to mcc.
解决方案:
删除matlab注释中的中文字符,并在matlab路径中全部使用英文路径。
4.在IDE中调用.jar文件异常
java.lang.UnsatisfiedLinkError: Failed to find the requiredlibrary mclmcrrt9_2.dll on java.library.path.This library is typically installedalong with MATLAB or the MATLAB Runtime. Its absence may indicate an issuewith that installation or the current path configuration, or a mismatch with thearchitecture of the Java interpreter on the path.
原因:未安装matlab runtime运行环境,或运行环境版本不匹配
解决方案:下载安装MCR环境(打包生成文件目录下\for_redistribution\ MyAppInstaller_web.exe双击下载;或者至matlab官网下载:https://www.mathworks.com/products/compiler/matlab-runtime.html 选择与matlab版本匹配的运行环境下载)
5.程序在IDE运行中崩溃。
错误日志:
------------------------------------------------------------------------
Assertiondetected at Wed Jun 13 20:09:19 2018
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Current Graphics Driver:Unknown hardware
Default Encoding : GBK
Deployed : true
Graphics card 1 : NVIDIA ( 0x10de ) NVIDIA GeForce GTX 1050Ti Version 23.21.13.9124 (2018-3-15)
Host Name : DESKTOP-19U3B14
MATLAB Architecture: win64
MATLAB EntitlementID: Unknown
MATLAB Root : D:\Program Files\MATLAB\MATLAB Runtime\v93
MATLAB Version : 9.3.0.713579 (R2017b)
OpenGL : hardware
Operating System : Microsoft Windows 10 专业版
Processor ID : x86 Family 6 Model 158 Stepping 10, GenuineIntel
Virtual Machine : Java 1.8.0_131-b11 with Oracle CorporationJava HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 10.0 (Build 16299)
Fault Count: 1
Assertion in foundation::usm::Detail<struct foundation::usm::scope::Mvm>::findat b:\matlab\foundation\usm\management.cpp line 559:
find: no active context for type 'struct `anonymous namespace'::DispatcherState'
Register State (captured):
RAX = 000000001eaf3338 RBX = 0000000000000000
RCX = 000000002b3799b0 RDX = 0000000000000000
···························································
MATLAB is exiting because of fatal error
原因:由于使用了盗版的matlab软件所致,注册为能够将matlab破解完全,所以导致运行时崩溃
解决方案:更换版本或更换注册机重新安装matlab。
6.调用matlab程序导致内存不断上涨。
原因:在调用matlab程序后未使用dispose()方法释放内存空间。
参阅matlab文档:
The dispose() method must be called on a Add instance when it is no longer needed to ensure that native resources allocated by this class are properly freed.
五、总结
对于matlab打包近阶段解决了很多意想不到的问题,在尝试过程中发现了官方文档以及生成日志的重要性,解决了项目中的各种问题,总结如上,分享给大家。
原创不易,转载请注明出处。