MATLAB Answers

1

How do I configure the Java run time library path (java.library.path) in MATLAB with and without admin privileges?

I need to have the MATLAB JVM load a native library (.dll file on Windows, .so file on Unix, and .dylib file on Mac).
How can I set the Java run time library path (java.library.path) in MATLAB so that the MATLAB JVM can pick up the custom library? I would like to be able to set it regardless of access to admin privileges.
If I use the Java setProperty method in MATLAB:
java.lang.System.setProperty('java.library.path', 'C:\TEMP')
java.lang.System.getProperty('java.library.path')
The above code does not change the libraries being used, although the property has changed:
ans =
C:\TEMP

1 Answer

Answer by MathWorks Support Team on 13 Oct 2016
 Accepted Answer

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:
 
<matlabroot>/toolbox/local/librarypath.txt
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:
 
java.lang.System.load('C:\libraries\mylib.dll');%Windows
java.lang.System.load('/Users/username/libraries/libMylib.so');%Unix
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:
 
<matlabroot>/bin/<arch>
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.

  0 Comments

Sign in to comment.