The java.lang.System.setProperty method will not work as the java.library.path variable is read during JVM start up (MATLAB start up) only.
There are several options to allow the MATLAB JVM to find a library.
*On MATLAB 7.0 (R14) and later***
1. Add the directory that contains the library to the following file:
where "matlabroot" is the MATLAB installation directory. MATLAB uses this file to set its JVM Java library path on start up. Each path entry must be on a new line in the file. MATLAB must be restarted after editing the file as it is only read during start up. Admin privileges may be necessary.
Alternatively, this file may be placed in the MATLAB start up directory, but this is not recommended. Having more than one librarypath.txt file per MATLAB installation is not recommended.
2. Preload the desired library using the MATLAB built-in Java interface. Once it is loaded into the JVM memory, the JVM should be able to utilize it without needing to find it on the library path. Use the following command in the MATLAB command window to preload:
While this method does not require admin privileges and does not require MATLAB restart, the changes are not persistent and you will need to reload the library after every MATLAB start up.
3. Place the desired library in the same folder as the MATLAB executable:
where "matlabroot" is the MATLAB installation directory and "arch" is the architecture (win64, glnxa64, etc.). This directory is always on the Java library path so any library placed here will always be visible to the JVM.
*On MATLAB 6.5 (R13) and earlier***
The Java library path is initialized using the PATH (on Windows) and LD_LIBRARY_PATH (on Linux, UNIX) environment variables. Set these variables to include the directory that holds the desired library, and then restart MATLAB. This may require admin privileges.