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?

Ivan
  • 7,448
  • 14
  • 69
  • 134
  • Possible duplicate of [Call a java function from matlab script](https://stackoverflow.com/questions/36989040/call-a-java-function-from-matlab-script) – vinS Dec 10 '17 at 05: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 – Ivan Dec 10 '17 at 05:20

1 Answers1

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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. – Ivan Dec 10 '17 at 17:11