MATLAB: Does the compiled application not locate a MATLAB built-in Java class with MATLAB Compiler 4.0 (R14)

builtclassclasspathinjavajavaclasspathm-codeMATLAB

I created an application and then compiled it with MATLAB Compiler. I am modifying the dynamic Java classpath in my MATLAB code using javaclasspath as follows:
javaclasspath('/usr/oracle/jdbc/classes12.jar')
My application works fine in MATLAB and on the development machine, but when I execute it on the deployed machine with the MCR, I receive the following error.
??? Undefined variable "com" or class "com.mathworks.toolbox.database.databaseConnect".
Error in ==> database.database at 78
Error in ==> testdb at 3
MATLAB:undefinedVarOrClass

Best Answer

MATLAB Compiler uses the dynamic class path to reference JAR files used by any toolbox functions used in your application. This is because the MATLAB Compiler Runtime (MCR) only includes the base MATLAB functions, so JAR-files associated with individual toolboxes are packaged in the CTF-file. When executing the application in MATLAB, these JAR-files are on the static Java classpath, while in compiled applications they are on the dynamic classpath.
If your application needs to modify the Java classpath, you should retain any entries in the existing classpath and add the new paths. You can do this using the JAVAADDPATH function. For example:
javaaddpath('/usr/oracle/jdbc/classes12.jar')
This will prepend the directory to the dynamic classpath, leaving intact any JAR-files automatically added to the dynamic path by MATLAB Compiler.
For more information on JAVAADDPATH execute the following command at the MATLAB command prompt
doc javaaddpath