Litterate
Litterate

Reputation: 135

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

Upvotes: 3

Views: 4380

Answers (2)

Marcin
Marcin

Reputation: 238209

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

Upvotes: 6

nicephotog
nicephotog

Reputation: 21

Your notation to the compiler of the constructor is a polymorphic class meaning "use appropriate constructor that is called at runtime".

public PerlinNoiseGenerator()

public PerlinNoiseGenerator(int seed)

The first form with no argument can be called but is irrelevent because the line with this(DEFAULT_SEED) attempts to call itself but only one constructor is allowed used

Second constructor has int for an argument but requires being loaded by an already loaded class.

Use the first version and change the case sensitive name of the one with the argument and remove this(DEFAULT_SEED) from it replace with the method name(the one you changed from a constructor that has the argument). e.g. public perlinNoiseGenerator(int seed)

note: by convention java code method names start with a lower-case letter.

A final note, java arguments from the command line come in as "String" data type through the "main" method, a starter method for applications (gui or command prompt). The first argument on the main method argument is the first commandline argument.

public static void main(String[] Args){
new PerlinNoiseGenerator(Args); // recursive class call
}//end main method

int[] args; // global
public PerlinNoiseGenerator(String[] Args){
int arglength=Args.length();
args = new int[arglength];
for(int cnt=0;cnt<arglength;cnt++){
Args[cnt].trim();
args[cnt]=new Integer(Args[cnt]).intValue();
}//enfor
perlinNoiseGenerator(args[0]); // call method
}//end constructor

Upvotes: 0

Related Questions