1

I want to to run java code in Matlab so I can make use of what I have learned in java to enhance my MATLAB codes.

1

1 Answer 1

3

Totally quoting from here

Suppose your Java program is like this:

public class MyClass {
    public static void main( String args[] ){}
}

To call this program in MATLAB:

  1. Outside of MATLAB: Compile this class, so you have file MyClass.class

  2. Locate the classpath.txt file for the MATLAB installation. The location of this file can be found by typing the following command in MATLAB command window:

    which classpath.txt

  3. Open the 'classpath.txt' with a text editor as Administrator. Add the full path for the directory with the MyClass.class to the end of the 'classpath.txt' file as a single line and save the file.

  4. Restart MATLAB.

  5. In MATLAB: to create an object of class MyClass, type:

    o = MyClass

  6. In MATLAB: to execute main() of object o, type:

    javaMethod('main', o, '')

Alternately one may also add the directory in which the class files are to the dynamic path. Use the JAVAADDPATH command to add the directory (which contains the MyClass.class file) to JAVA's dynamic classpath. This also obviates the need to restart MATLAB. Once this is done the code can be invoked as follows:

o = MyClass;
javaMethod('main', o);

For a detailed tutorial please see this.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, i have added this to the end of the classpath.txt file : # Java classpath entries for C:\Users\Ahmed Gad\Documents\NetBeansProjects\Tree\build\classes\tree\TreeExample.class but this doesn`t work when trying to take an object o=TreeExample;
@AhmedGad> You're welcome, I suggest you to read out the chapter 7 (page 321) of the tutorial which I've pointed at cause it will help you in advance and you can fix a verity of issues that may cause problems for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.