Eclipse java调用Matlab2015b编译的jar包
2017-06-16 20:46
357 查看
Matlab2015b支持jdk1.7,和1.8版本不兼容,因此电脑上要装jdk1.7并进行环境变量配置。我使用的是eclips4.5,可是eclips4.5只支持jdk1.8,因此可以选择装两个版本的jdk,只要切换了jdk环境变量配置,然后重启Matlab2015就可以了。至于装多版本jdk的方法我也是在网上找的,连接如下:
多版本jdk安装方法链接(转载):http://blog.sina.com.cn/s/blog_437ee9d00101isob.html
1、配置
matlab2015b,jdk1.7(jdk1.8不兼容), eclipse4.5。
2、例子
(1)新建两个matlab文件;如下所示: //代码是在网上找的,偷了个懒
function out = plotcos(first,step,last)
in = first:step:last;
out = cos(in);
figure('Name','Math Plot - Cos Function','NumberTitle','off');
plot(in, out, 'b-');
axis([first last -1.5 1.5]);
grid on;
title('cos
4000
函数曲线');
function out = plotsin(first,step,last)
in = first:step:last;
out = sin(in);
figure('Name','Math Plot - Sin Function','NumberTitle','off');
plot(in, out, 'b-');
axis([first last -1.5 1.5]);
grid on;
title('cos函数曲线');
(2)编译.jar文件
使用deploytool命令调出编译对话框。
选择第三项。
添加包名,函数以及类名等。
点击右上绿色对号,开始编译。
编译完成。
找到两个文件,需要拷到java工程里,一个是编译好的matlabplot.jar文件,和matlab根目录下的javabuilder.jar包,路径及文件如下图所示:
(3)新建java工程,导入jar文件
在工程名右击,新建文件夹,命名为lib。
将前面找到的两个文件拷贝到lib文件夹
将jar库文件加入依赖。
(4)实例化运行程序
package javajar;
import matlabplot.*; //导入包名
public class test {
public static void main(String[] str)
{
try{
Mathplot func = new Mathplot();
func.plotcos(0, 0.0f, 0.01f, 10*Math.PI);
func.plotsin(0, 0.0f, 0.01f, 10*Math.PI);
}catch (Exception e){
e.printStackTrace();
}
}
}
(4)效果
注:若matlab函数有输出,则写为以下形式:
function out = addsum(a ,b) //输出一个参数
out = a + b;
end
function [out1,out2] = getnum(a ,b) //输出两个参数
out1 = a + b;
out2 = a - b;
end
java调用的时候:
try {
Class1 func = new Class1();
Object[] a = func.addsum(1,12, 13);
Object[] b = func.getnum(2,18, 12);
int aa = Integer.valueOf(String.valueOf(a[0])); //object转为int
System.out.println(a[0]);
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(aa);
} catch (MWException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
多版本jdk安装方法链接(转载):http://blog.sina.com.cn/s/blog_437ee9d00101isob.html
1、配置
matlab2015b,jdk1.7(jdk1.8不兼容), eclipse4.5。
2、例子
(1)新建两个matlab文件;如下所示: //代码是在网上找的,偷了个懒
function out = plotcos(first,step,last)
in = first:step:last;
out = cos(in);
figure('Name','Math Plot - Cos Function','NumberTitle','off');
plot(in, out, 'b-');
axis([first last -1.5 1.5]);
grid on;
title('cos
4000
函数曲线');
function out = plotsin(first,step,last)
in = first:step:last;
out = sin(in);
figure('Name','Math Plot - Sin Function','NumberTitle','off');
plot(in, out, 'b-');
axis([first last -1.5 1.5]);
grid on;
title('cos函数曲线');
(2)编译.jar文件
使用deploytool命令调出编译对话框。
选择第三项。
添加包名,函数以及类名等。
点击右上绿色对号,开始编译。
编译完成。
找到两个文件,需要拷到java工程里,一个是编译好的matlabplot.jar文件,和matlab根目录下的javabuilder.jar包,路径及文件如下图所示:
(3)新建java工程,导入jar文件
在工程名右击,新建文件夹,命名为lib。
将前面找到的两个文件拷贝到lib文件夹
将jar库文件加入依赖。
(4)实例化运行程序
package javajar;
import matlabplot.*; //导入包名
public class test {
public static void main(String[] str)
{
try{
Mathplot func = new Mathplot();
func.plotcos(0, 0.0f, 0.01f, 10*Math.PI);
func.plotsin(0, 0.0f, 0.01f, 10*Math.PI);
}catch (Exception e){
e.printStackTrace();
}
}
}
(4)效果
注:若matlab函数有输出,则写为以下形式:
function out = addsum(a ,b) //输出一个参数
out = a + b;
end
function [out1,out2] = getnum(a ,b) //输出两个参数
out1 = a + b;
out2 = a - b;
end
java调用的时候:
try {
Class1 func = new Class1();
Object[] a = func.addsum(1,12, 13);
Object[] b = func.getnum(2,18, 12);
int aa = Integer.valueOf(String.valueOf(a[0])); //object转为int
System.out.println(a[0]);
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(aa);
} catch (MWException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
相关文章推荐
- Java调用Matlab编译生成jar时问题
- matlab生成jar eclipse正常调用jar
- Java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案
- C#调用java类、jar包方法;eclipse生成jar包
- 用Eclipse编写Java时调用algs4.jar和stdlib.jar解决StdOut和StdIn无法被识别的问题
- JAVA程序调用Matlab神经网络工具编译的jar包注意问题
- 调用eclipse的JDT编译java源代码
- Java调用MATLAB程序打包的jar包时出现错误MWEException
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
- 使用GCJ编译Java程序供Matlab调用Java对象方法实践
- java/eclipse调用matlab功能函数
- JAVA学习(15)Vs2015+eclipse编译Qt动态库模块
- matlab2014调用vs2015进行混合编译生成mex文件
- eclipse调试java调用matlab程序的7.17dll找不到的问题
- 不用ide编译java程序时调用jar包
- Java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案
- java调用matlab生成的jar
- C#调用exe解决PDF转图片问题(使用PDFBox方案,java语言编译jar包实现)
- 如何使用matalb中的deploytool函数打包jar包嵌入eclipse供java程序调用
- 不用ide编译java程序时如何调用jar包