0

Trying to call this function from matlab

package a.b;

public class TestFunction { 
public TestFunction(){
}
public static void HelloWorld() {
 System.out.println("Hello, World");
 }
} 

I compiled it and got a class file:

[idf@localhost b]$ ls
TestFunction.class  TestFunction.java
[idf@localhost b]$ pwd
/home/idf/Documents/java/a/b
[idf@localhost b]$ 

I added the path to the parent directory in matlab

javaaddpath('/home/idf/Documents/java/')

If I try to import the package or call it I get errors:

>> import a.b
Error using import
Import argument 'a.b' cannot be found or cannot be imported.

How do I call the java function TestFunction.HelloWorld from matlab?

2
  • Possible duplicate of Call a java function from matlab script Commented Dec 10, 2017 at 5:05
  • That's helpful. It looks like the java that I am using, Java build 1.8.0_151-b12, is not the same as the one in matlab: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode Commented Dec 10, 2017 at 5:20

1 Answer 1

1
>> import a.b
Error using import
Import argument 'a.b' cannot be found or cannot be imported.

This tells Matlab to import a class called b from a package called a. To import all classes in the package a.b, do this:

>> import a.b.*

To import just TestFunction

>> import a.b.TestFunction

Reference: matlab documentation for import

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

1 Comment

Thanks, along with the above comment by vinS, I was able to resolve issue. I had to 1) change the MATLAB_JAVA export to point to the new Oracle java, javaaddpath to the actual directory where compiled class resides, then use your post.

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.