java使用matlab,在Matlab中使用Java类

I've been struggling with this problem for two days now and no resource I've found have been able to solve it.

I am trying to call a java class (added the link at the bottom) from Matlab (version 7.13.0.564 (R2011b)). I've compiled the java class using java 1.6 into a .class file and also added the path to the folder where the file is situated using javaaddpath (I've of course checked that the path is correct in the list of dynamic paths). However, when I try to call the class from Matlab using javaMethod('main','PerlinNoiseGenerator','') I get the error:

"No class PerlinNoiseGenerator can be located on Java class path"

I would be extremely grateful if someone with experience in calling java from Matlab could put together a short tut on how to do this. I am probably going to distribute my code so I kinda need to set the java path dynamically and from what I've read it really should be possible although I've seen post that indicate that it could be the cause of the problem.

解决方案

Usually I create jar files that contain java classes. I also had problems loading individual java classes before. In your case I did the following on xubuntu 13.04 x64 and Matlab 2013a x64 to load your particular class:

Compile it using java 6 (not the default 7) with option -d . to create a set of package folders, as your class defines a package org/j3d/texture/proecedural/ etc:

/usr/lib/jvm/java-6-openjdk-amd64/bin/javac -d . PerlinNoiseGenerator.java

This will compile the class and make in the current director (thus .) the set of package folders.

Make jar file containing your class again using jar from java 6. I named it javaNoise.jar:

/usr/lib/jvm/java-6-openjdk-amd64/bin/jar cf javaNoise.jar ./org/j3d/texture/procedural/PerlinNoiseGenerator.class

In Matlab, in the directory where javaNoise.jar is:

javaaddpath('./javaNoise.jar');

Create object of your java class:

png=org.j3d.texture.procedural.PerlinNoiseGenerator()

% results in: png = org.j3d.texture.procedural.PerlinNoiseGenerator@3982a033

To test it, I just generated some 1D noise:

png.noise1(1.2)

ans = -0.0960

Hope this helps.

P.S.

javaMethod('main','PerlinNoiseGenerator','') wont work because this class has no main method:-).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值