Deployed Java package: Does "getenv" get value inheritted from immediate parent process?
4 views (last 30 days)
Show older comments
I used Compiler SDK to package an m-file function "ABC" as a Java object and method. When the client Java app instantiates the object to use the method, the MATLAB Runtime is used. My function uses the MATLAB code 'getenv("ABC_ifDebug")' to decide whether to print diagnostics. The deployed "jar" file is used on both Windows and Linux.
The 'getenv' documentation says "returns the value of the operating system environment variable `varname`". This implies that it actually asks the OS for the value of `varname`, i.e., it doesn't use the value inheritted from the parent process. This differs from my understanding of environment variables, which is *inheritted* from the immediate parent process.
The documentation further says "On UNIX® platforms, the shell you use to start MATLAB® determines the operating system environment". This again differs from inheritting the value from the immediate parent.
Is the documentation accurate? If so, it means that MATLAB's treatment of environment variables differs from my decades long understanding (or misunderstanding) of how environment variables work.
I'm tagging this as a MATLAB question because I suspect that the behaviour of "getenv" is the same when using MATLAB versus the runtime. The difference is the context, i.e., for the runtime, the immediate parent might not be the shell or the OS. Or maybe it is but it's just not obvious to me.
0 Comments
Accepted Answer
Aravind
on 24 Dec 2024
In MATLAB and the MATLAB Runtime, environment variables are inherited from the parent process, which is consistent with standard operating system behavior. The "getenv" function accesses these variables as they exist in the MATLAB process environment, reflecting inherited values unless explicitly modified during the MATLAB session.
The documentation's mention of the shell on UNIX platforms indicates that the environment variables set in the shell are what MATLAB will initially recognize when it starts. This does not conflict with the inheritance model; it simply clarifies that the shell's environment is the source of these variables.
When deploying with MATLAB Compiler SDK, the MATLAB Runtime will inherit environment variables from the process that starts it, typically your Java application. To ensure consistent behavior, make sure the environment variable "ABC_ifDebug" is set before launching your Java application on both Windows and Linux.
I hope this clarifies your question.
More Answers (0)
See Also
Categories
Find more on C Shared Library Integration 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!