---文章比较啰嗦----是我的分析过程-----请大家选择性挑选自己需要的部分看参考---
-----还有我只涉及java在后端调用matlab的过程,不涉及前端
----经验总结:
(1)我用的是R2018a,java是1.8的,就是用网上的普通破解方法破解之后就可以直接用
---有很多博客说java,要1.7---no----matlab现在可以兼容到1.8了
---有一些文章说,要完全破解,我找了很久的完全破解的文件,没有R2018a的,然后我直接跑就通过了
----总结,如果你是R2018a,java是1.8的,不用再折腾了,很有可能现在就能用了
(2)剩下的操作步骤,我是借鉴一个博客写的
https://blog.csdn.net/lan_yu22/article/details/81269890
(给无私的大佬鼓掌...)
按照这个生成jar包,然后把这个包和javabuilder的包都配置到java web 环境中~
然后web项目中添加一个类,来调用这个函数,我依旧是照着上面哪个博客写的,但是有一个地方
Object result[] = test. test_connection (1 ,11);
请看上面这行,是我改之后的调用matlab函数的部分,我写的matlab函数是
function [outputArg1] = test_connection(x)
outputArg1 = x+1;
end
所以为什么 我的m函数只需要一个输入参数,而我要在java调用的时候写两个参数呢??
emm,经过笨蛋式测试,我发现第一个参数是来指定输出参数的个数的。。。
比如我把 第一个参数改成2的时候,eclipse会提示我 “输出参数过多”
还有为什么输出只有一个还有用 result[ ]来接收结果,然后用result[0]来输出结果呢??
emm,经过测试,当我直接用result接收并输出时,eclipse会输出:“[Ljava.lang.Object;@5e8c92f4” (大概是地址??)
反正就是老老实实 按照样例写啦
然后最后我这里是可以正常运行的~~
然后最后附上我测试的完整的java代码把
import com.mathworks.toolbox.javabuilder.MWException;
//这个是我们导入的javabuilder的工具包
import test_connection.Class1;
//这个是我们导入的matlab代码生成的工具包
public class test_connetction {
public static void main(String[] args) {
try {
Class1 test = new Class1();
Object result= test. test_connection (1 ,11);
System.out.println((int)result);
} catch (MWException e) {
e.printStackTrace();
}
}
}
而且百分百确定能跑:
------好了,看到这里,你就可以自己操作了,下述的文字都是废话(千万别看,浪费时间)-------
java调用matlab 主要基于 matlab安装后文件中toolbox中的javaBuilder。
贴心的开发人员已经把例子写出来咯...
我主要是开发java Web,---->java_Web_vararg_demo
文件结构如下
(额...忽略红色错误)
首先分析---有三个部分:
1.前端jsp,html文件:只包括向java后端传递参数的部分,没有直接接触matlab
(主要用session的setAttribute()方法来向后端传递数据)
2.java代码:---一段一段来分析-----
package VarArg;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Necessary package imports for using ML component
import com.mathworks.toolbox.javabuilder.*;
import com.mathworks.toolbox.javabuilder.webfigures.*;
import com.mathworks.toolbox.javabuilder.web.MWHttpSessionBinder;
// ml created component
import vararg_java.*;
----注意需要导入的包~~~ 初步判断导入 javabuilder.jar到自己的项目中就ok了
vararg_javaclass fComponentInstance = null;
public void init() throws ServletException
{
if(fComponentInstance == null)
{
try
{
fComponentInstance = new vararg_javaclass();
}catch (Throwable t)
{
t.printStackTrace();
}
}
}
public void destroy()
{
if(fComponentInstance != null)
fComponentInstance.dispose();
}
----定义初始化和清除的函数,大概可以直接套用,I guess .....
下面一个函数一个函数来分析
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Object[] outputArray = null;
FormData formData = extractDatafromForm(request);
WebFigure wb;
if ( !formData.valid() )
{
printHTMLError(response);
return;
}
try
{
outputArray = fComponentInstance.varargexample(formData.getVarOutputs(),
formData.getDataArray(), formData.getVarInputs() );
wb = (WebFigure)MWJavaObjectRef.unwrapJavaObjectRefs(outputArray[0]);
// Set the figure scope to session
request.getSession().setAttribute("Vararg_Figure",wb);
// Bind the figure's lifetime to session
request.getSession().setAttribute("Vararg_Figure_Binder", new MWHttpSessionBinder(wb));
printHTMLOutput(request,response,wb,outputArray);
// You can use the viewer (view.jsp) by uncommenting the following code and commenting the call to
// printHTMLOutput(request,response).
// updateSession(request.getSession(),outputArray);
// RequestDispatcher dispatcher = request.getRequestDispatcher("/view.jsp");
// dispatcher.forward(request, response);
}
catch(Throwable t)
{
t.printStackTrace();
}
finally
{
MWArray.disposeArray(outputArray);
}
}
这个函数,是对于前端传来的数据进行接收,然后经过处理后又传输到前端,所以我们提取出来,将接收到的数据进行处理的部分,如下:
Object[] outputArray = null;
FormData formData = extractDatafromForm(request);
WebFigure wb;
outputArray = fComponentInstance.varargexample(formData.getVarOutputs(),
formData.getDataArray(), formData.getVarInputs() );
wb = (WebFigure)MWJavaObjectRef.unwrapJavaObjectRefs(outputArray[0]);
----这个部分应该就是java调用matlab 的代码,重点做标记!!!!!
接着分析:
后面的函数 ....根据名字就可以判断和调用matlab无关了...
下面是这个样例调用的matlab函数。
function [w_fig, varargout] = varargexample(Data, varargin)
% VARARGEXAMPLE used varargin and varargout for demonstration purposes
%
% The purpose of VARARGEXAMPLE is to demonstrate integration of MATLAB
% created components using varargin and varargout. It also demonstrates
% the use of webfigures for embedding MATLAB graphics in web pages.
%
% The function VARARGEXAMPLE takes one required input, the data to plot.
% The function then takes a variable number of token value pairs. These
% pairs set properties of the figure and plot to be created.
% Syntax:
% varargexample(Data)
% varargexample(Data, PropertyName, PropertyValue, ...)
所以就是java代码里通过 fComponentInstance调用了这个matlab的函数完成了对接。
??提出问题:那么matlab是把这个函数文件打包成jar包了么??
emm所以应该不是?