3

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

Litterate
  • 135
  • 1
  • 6
  • perhaps this will help: [http://stackoverflow.com/questions/8499890/matlab-cannot-see-some-of-my-java-classes-not-all-in-jar-package?rq=1](http://stackoverflow.com/questions/8499890/matlab-cannot-see-some-of-my-java-classes-not-all-in-jar-package?rq=1) – Lucius II. Jul 25 '13 at 09:15
  • Thanks, but unfortunately this was not the issue. I tried a second script that didn't call any external libraries and I still cannot find the class. – Litterate Jul 25 '13 at 09:29
  • another try: [http://stackoverflow.com/questions/1251695/how-do-i-use-user-defined-java-classes-within-matlab?rq=1](http://stackoverflow.com/questions/1251695/how-do-i-use-user-defined-java-classes-within-matlab?rq=1) --> there are several hints, what could casue the error... – Lucius II. Jul 25 '13 at 12:00

2 Answers2

6

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

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • @Litterate Glad I could help. If so, accepting the answer would be useful:-) – Marcin Jul 25 '13 at 21:02
  • +1 for the clear instructions. From what I understand, MATLAB ships with Oracle Java 6 JRE, would that make a difference with regard to using OpenJDK to compile? – Amro Jul 26 '13 at 02:11
0

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
nicephotog
  • 21
  • 2