Deployed Java package: Does "getenv" get value inheritted from immediate parent process?

4 views (last 30 days)
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.

Accepted Answer

Aravind
Aravind on 24 Dec 2024
Hi @FM,
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.
  1 Comment
FM
FM on 24 Dec 2024
Edited: FM on 24 Dec 2024
Thank you, Aravind. It sounds to me like the documentation could be refined. If the runtime gets its environment variables from the parent process, then it is possible that the parent process has made this value different from that set in the OS. For example, in Windows, there is an OS-wide setting of environment variables. If the runtime directly queries the OS for the value of the variable, it would be different from that inheritted from the parent process. Rather than saying "returns the value of the operating system environment variable", it would be more accurate to say that getenv returns the value inheritted from the parent process.

Sign in to comment.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!