How can compiled applications inherit a custom Java class path and use the Database Toolbox?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 14 Aug 2023
Edited: MathWorks Support Team
on 3 Jul 2024
My MATLAB code connects to a JDBC-based database, with a JAR driver file. Now I want to deploy my code as standalone applications or web apps that run on MATLAB Web App Server. The deployed application should be able to access the JAR driver file as well. What do I need to do to make this work?
Alternatively, my application uses a custom Java package JAR file, and I set the Java class path using "javaclasspath.txt" in the Preferences directory specified by "prefdir". How does this work if I want to compile the application?
Accepted Answer
MathWorks Support Team
on 1 Jul 2024
Edited: MathWorks Support Team
on 3 Jul 2024
To set up a Java JAR file for deployed applications, there are similar approaches. But you need to install the driver JAR file on the target machine first. You also need to make sure the user who will run the deployed application can access the JAR file. For example, if you are running MATLAB Web App Server, the "MwWebAppsWorkerR20xxx" account must have at least read and executable access to the JAR file.
There are multiple ways to set up Java JAR files in MATLAB:
1. Add directory to the JAR file to <mcr root>\toolbox\local\javaclasspath.txt.
This is not a recommended workflow.
2. Include a "javaclasspath.txt" file from preference folder in the compiled application (folder returned by "prefdir" command).
Before R2022a, the "javaclasspath.txt" file in the MATLAB preference folder was automatically packaged in the deployed application. If the JAR file on the target machine is in the same folder, you do not need to do anything. If the JAR file is placed in a different folder on the target machine, you need to modify the "javaclasspath.txt" file before packaging.
Starting in R2022a, MATLAB Compiler became more selective about which files to install from the user's preference folder into the deployed application. "javaclasspath.txt" and "javalibrarypath.txt" are not automatically included. To continue using "javaclasspath.txt", you need to explicitly add the file to the deployed application using the "mcc -a" option, or through the "deploytool" interface. Again, if the JAR file is placed in a different folder on the target machine, you need to modify the "javaclasspath.txt" file before packaging.
3. Use "javaaddpath" to add the JAR file directory on the target machine.
You can use the "isdeployed" function for the deployed app.
4. If you cannot install the jar file on the target machine:
Use the "mcc -a" option or "deploytool" interface to add the JAR file to the deployed application. JAR files added with "-a" option or "deploytool" are automatically added by "javaaddpath" during the initialization of the CTF file. This is the recommended approach as you do not need to install the jar file separately on the target machine and the users are guaranteed can access the JAR file.
If you are using "javalibrarypath.txt" file to point to DLL files that are used in the application, you need to explicitly use the "mcc -a" option or "deploytool" to add the "javalibrarypath.txt" file from the preference folder. Note that it must be added from the preference folder so that it will also be packaged into the preference folder of the CTF file. In addition, you need to have the DLL files available on the target machine. Please refer to Approach (2) above for more details.
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!