Clear Filters
Clear Filters

Can I use custom macros to define the path to my native methods library?

2 views (last 30 days)
My MATLAB code uses a Java class that dynamically loads native methods using the Java method java.lang.System.loadLibrary("LibFile"). I am using a "javalibrarypath.txt" file to add the location of the native method library to the Java Library Path, as outlined in this documentation:
The path to the native method library changes often, depending on which version I am using. It is tedious to change the hard-coded path in the "javalibrarypath.txt" file each time. Is there a way to encode the path with custom macros, similar to the $matlabroot, $arch, and $jre_home documented macros?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Apr 2024
MATLAB only supports the documented macros $jre_home, $matlabroot and $arch in javalibrarypath.txt. These macros are recognized in the source code: $jre_home matches the location sys/java/jre by default, $matlab_root is the same as matlabroot, and $arch is win64 on Windows etc. It is not possible to use custom macros. However, there are two possible ways to make it easier to switch between different library paths.
1. Instead of using the "javalibrarypath.txt", the customer can use LD_LIBRARY_PATH to manage the library versions. For example:
setenv LD_LIBRARY_PATH/my/native/library/folder/v1
matlab -r myJavaApp
setenv LD_LIBRARY_PATH/my/native/library/folder/v2
matlab -r myJavaApp
2. Create one "javalibrarypath.txt" for each version, and start MATLAB from the corresponding folder. For example, create a "javalibrarypath.txt" file in folder v1 and v2 for the desired libraries and do the following:
cd /my/native/library/folder/v1
matlab -r myJavaApp
cd /my/native/library/folder/v2
matlab -r myJavaApp

More Answers (0)


Find more on Java Package Integration in Help Center and File Exchange


No tags entered yet.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!