Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java class in Matlab

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

like image 275
Litterate Avatar asked Nov 19 '25 18:11

Litterate


1 Answers

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

like image 96
Marcin Avatar answered Nov 21 '25 07:11

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!