4

I encountered a problem when I tried to use java from matlab. I read through the tutorials from MathWork.com several times, also I re-installed the JDK1.6, in order to be compatible with matlab. However, after my work, it still doesn't work...

Here is the contents in classpath.txt:

    C:\Program Files\MATLAB\R2010a\java\jarext\xstream.jar                                  
    C:\Program Files\MATLAB\R2010a\toolbox\javabuilder\jar\win64   \javabuilder.jar            

    DYNAMIC JAVA PATH

C:\Users\Gao\Desktop\connected_components_labeling

Clearly, the directory is included in the file. The connected_component_labeling is just a folder on my disk. The classes that I want to use in the connected_components_labeling are: Disjoint_Set.class and Node.class are in the connected_components_labeling folder.

I tried:

x =  Disjoint_Set();

also

x = connected_components_labeling.Disjoint_Set();

None of them work. The only feedback I got from matlab is:

??? Undefined variable "connected_components_labeling" or class
"connected_components_labeling.Disjoint_Set".

I'm pretty frustrated. Could anyone help me out? I'd appreciate it. Thanks a ton!

Amro
  • 123,847
  • 25
  • 243
  • 454
whileone
  • 2,495
  • 3
  • 21
  • 30
  • Is "connected_components_labeling" a package? Are the classes in a jar file? Or are they simply .class files sitting in that directory? Were those classes perhaps already in a package and then removed from it? – kenm Jun 15 '12 at 15:44
  • I just edited it. The connected_components_labeling is a package. The classes are not in a jar file. They are just .class files.Sorry for the confusion. – whileone Jun 15 '12 at 15:47
  • If connected_components_labeling is actually a package, it shouldn't be listed on the path. The directory that it lives in should be on the path instead. So the Desktop directory here. Probably not a great place for it. – kenm Jun 15 '12 at 15:52
  • @Gao: You should use `javaaddpath` to add entries to the Java class path – Amro Jun 15 '12 at 16:25
  • @Amro: I tried javaaddpath(). This function will add the files to dynamic path. And it also doesn't work. – whileone Jun 15 '12 at 17:22

1 Answers1

4

Make sure that you are compiling the java files using a JRE/JDK that MATLAB is compatible with. As far as I can tell, MATLAB does not work properly with Java 7, so stick with Java 6 for the moment...

There are a couple of environment variables that affect MATLAB. In my case I have:

JAVA_HOME = C:\Program Files\Java\jdk1.6.0_32
MATLAB_JAVA = C:\Program Files\Java\jre6
PATH = ...;C:\Program Files\Java\jdk1.6.0_32\bin

Here is a simple test I just did:

C:\work\Student.java

public class Student {
    private String name;
    public Student(String str) {
        name = str;
    }
    public void setName(String str) {
        name = str;
    }
    public String getName() {
        return name;
    }

    public static void main(String args[]) {
        Student s = new Student("amro");
        s.setName("unknown");
        System.out.println("Hello " + s.getName());
    }
}

I compile: javac Student.java (the output is placed in the same directory c:\work\Student.class). Now I test it from MATLAB:

javaaddpath('C:\work')

javaMethod('main','Student','')

s = Student('me')
char( s.getName() )

I get:

Hello unknown

s =
Student@8d6877

ans =
me
Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    +1 for checking that your JDK version is compatible with your MATLAB version. Fixed my problem. Thanks! Here's a [list of supported JDK versions for each MATLAB version](http://www.mathworks.com/support/sysreq/previous_releases.html) (click on the "Supported Compilers" link). – Florian Brucker Dec 12 '13 at 10:31
  • @FlorianBrucker: From what I understand there are [workarounds](http://undocumentedmatlab.com/blog/using-java-7-in-matlab-r2013a-and-earlier/).. But you don't have to worry about that anymore, you'll be happy to know that MATLAB R2013b finally upgraded to Java 7. – Amro Dec 12 '13 at 10:35