Use java in matlab

2020-02-12 09:11发布

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!

1条回答
Luminary・发光体
2楼-- · 2020-02-12 10:00

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
查看更多
登录 后发表回答