MATLAB: Can I not import the Java classes that are declared as part of a package

classimportjavajavaaddpathMATLABmethodsviewpackage

If I have a class named "test.class" that is declared as part of package "pack", I get an undefined function error when I try to import the file.

Best Answer

The following code produces an "undefined function or variable 'test'" error:
javaaddpath('<root_directory>\pack');
import test.*
methodsview(test)
The source of the error is that a Java package is similar to directory. Although the directory that contains the class file has been added to the Java path, the class is still expected to be contained in a subdirectory "pack". You can fix this problem using the following code:
javaaddpath('<root_directory>');
import pack.test.*
methodsview(test)
If 'pack' is a JAR archive (.jar file), it may also be added to the Java class path as follows:
javaaddpath('<root_directory>\pack.jar');