在Matlab中使用Java类 [英] Using Java class in Matlab

查看:533
本文介绍了在Matlab中使用Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题两天,现在没有资源,我发现已经能够解决它。



我试图调用Java类(添加了底部的链接)从Matlab(版本7.13.0.564(R2011b))。我已经将java类使用java 1.6编译成.class文件,并且使用javaaddpath添加了文件所在文件夹的路径(我当然检查动态路径列表中的路径是否正确)。然而,当我尝试从Matlab使用javaMethod('main','PerlinNoiseGenerator','')调用类时,我得到错误:


没有类PerlinNoiseGenerator可以位于Java类路径上


有人在从Matlab调用java的经验可以放在一起一个短小的如何做到这一点。我可能要分发我的代码,所以我需要动态设置的java路径,从我已经阅读它应该是可能的,虽然我已经看到post,表明它可能是问题的原因。 p>

http://svn.j3d.org/code/tags/Xj3D-M10/src/java/org/j3d/texture/procedural/PerlinNoiseGenerator.java

解决方案

通常我创建的jar文件包含java类。我还有问题加载单个java类之前。在你的情况下,我做了以下在xubuntu 13.04 x64和Matlab 2013a x64加载您的特定类:


  1. 使用java 6(不是默认值7)用选项 -d。创建一组包文件夹,因为你的类定义了一个包 org / j3d / texture / proecedural / etc:



    / usr / lib / jvm / java-6-openjdk-amd64 / javac -d。 PerlinNoiseGenerator.java
    这将编译类并在当前director中创建(因此)包文件夹集。 p>


  2. 使用 jar 从java中重新生成包含您的类的jar文件6.我命名为 javaNoise.jar



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


  3. 在Matlab中, javaNoise.jar 是:



    javaaddpath('./ javaNoise.jar');


  4. 创建您的java类的对象:



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



    %results in:png =


  5. 要测试它,我只是产生了一些1D噪声:



    png.noise1(1.2)



    ans = -0.0960


希望这会有帮助。



PS
javaMethod('main','PerlinNoiseGenerator','')无法工作,因为这个类没有主方法:-)。


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.

http://svn.j3d.org/code/tags/Xj3D-M10/src/java/org/j3d/texture/procedural/PerlinNoiseGenerator.java

解决方案

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:

  1. 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.

  2. 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

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

    javaaddpath('./javaNoise.jar');

  4. Create object of your java class:

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

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

  5. 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:-).

这篇关于在Matlab中使用Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆