MATLAB Answers

How do I configure the Java Classpath and Librarypath when deploying my code to MATLAB Production Server or MATLAB Web App Server?

55 views (last 30 days)
I have a piece of MATLAB code which relies on Java functionality which in turn has some native dependencies (e.g. when working with Database Toolbox and are connecting to a Microsoft SQL Server with Windows Authentication through its JDBC driver). In order to make this work correctly inside MATLAB I know I need to load the JAR-file into MATLAB using "javaaddpath" or by making use of javaclasspath.txt in my preferences directory and I need to configure javalibrarypath.txt (also in preferences directory) to specify where the native libraries which the Java code depends on can be found.
Also I found this article on how to configure all this when deploying my code as standalone application:
But what exactly is the best workflow when deploying to MathWorks server based products like MATLAB Production Server and MATLAB Web App Server?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Jul 2020 at 4:00
1. To make the JAR-file itself available to code deployed to MATLAB Production Server or MATLAB Web App Server there are two major options and one less ideal option:
a. Similar to when deploying your code as a standalone application, all JAR-files which are compiled into your component (added to the "Files required for your app/archive to run" in the Compiler Apps or added to the "mcc" command line using the -a option) will automatically be loaded on the Java classpath of your deployed component. So basically that is all you have to do; package the JAR-file into the component at compile time.
b. Alternatively, if you want to make a particular JAR-file available to all components deployed to your server without having to include it with each separate component, you can add the JAR-file to the static classpath of the MATLAB Runtime which your server runs against. In the MATLAB Runtime installation which your server runs against, inside the toolbox\local directory, there is a file classpath.txt, edit this file and add the absolute location of the JAR-file which you want to add to the list (this works the same as javaclasspath.txt in MATLAB). Note that after updating classpath.txt you need to restart your server instance for these changes to take effect.
c. Theoretically you can also make your JAR-file available by making use of a javaclasspath.txt in the preferences directory on your development machine which is then compiled into the component and used at runtime of the component. What you would have to do then though is, on your development machine, enter an absolute path to the JAR-file which in the end actually needs to be valid on the server; which requires some planning ahead and is not very flexible. Also, if you ever need to change the location of the JAR-file on the server (or you want to deploy your component to a different instance on a different server with a completely different filesystem) you would have to go back to your development machine, change the path, recompile the component and then redeploy the component. With option b. you would at least have been able to just reconfigure the path in a plain-text file on the server without having to go back to the development machine and without having to recompile. And option a. is of course even more flexible. So this option c. would really be the least portable option.
2. To configure the Java librarypath to make the native libraries available to your Java classes on MATLAB Web App Server and MATLAB Production Server there is one preferred option and one alternative option:
a. This can also be configured in the MATLAB Runtime which your server runs against. Use librarypath.txt from the toolbox\local directory of that MATLAB Runtime installation. You enter the full absolute path to the location which contains the native libraries (similar to how javalibrarypath.txt is configured in MATLAB). The server instances needs to be restarted for any changes made to this file to take effect.
b. Theoretically this can also be done with javalibrarypath.txt in the preferences directory on your development machine. But just like with 1 c. this means that on your development machine you will have to hard code a path which in the end will have to exist on the server. And again, if you ever need to change the location on the server, you need to go back to the development machine, update the file, recompile and redeploy. The previous option would in that sense be more flexible again, you could just make the change on the server there.


Sign in to comment.

More Answers (0)

Translated by