How to make MATLAB read the environment variable JAVA_HOME

My question is how do I make MATLAB read the environment variable JAVA_HOME.
I enter the Matlab command
jenv
and get the following answer
ans =
JavaEnvironment with properties:
Version: "Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode"
Home: "/Applications/MATLAB_R2023a.app/sys/java/jre/maci64/jre"
Library: "/Applications/MATLAB_R2023a.app/sys/java/jre/maci64/jre/lib/server/libjvm.dylib"
Status: loaded
Configuration: "factory"
I enter the Matlab command
getenv JAVA_HOME
and get the following answer
ans = 0x0 empty char array
So I realize that for some reason JAVA_HOME has not been set properly. So I proceed to set the environement variable JAVA_HOME as instructed in:
I am running MATLAB on a Mac computer running IOS version 15.2, so according to the instructions I enter the following in ~/.zprofile
export JAVA_HOME="/Applications/MATLAB_R2023a.app/sys/java/jre/maci64/jre"
PATH="$JAVA_HOME/bin:${PATH}"
I do a echo $JAVA_HOME and I see the correct value for JAVA_HOME
but after restarting Matlab and entring in the command window
getenv JAVA_HOME
I still get
ans = 0x0 empty char array
Can anyone let me know what I am doing wrong, that MATLAB does not get the environment variable JAVA_HOME?

5 comentarios

The issue here is that when MATLAB is started from icon or dock, then it is not being invoked through an interactive shell. The approaches you have been using only set environment variables for interactive shells.
The documented method is to use launchctl setenv . However, settings done through this manner are lost upon reboot. I am having trouble finding information about setting persistent environment variables for OS-X Yosemite (10.10) and later. (Other than path -- for that use lauchctl config )
@Walter Roberson, as you are aware I "know nuthink!" about Mac OS X, but curiosity struck...I found an old <Stack Exchange thread> with a relatively late addition to it that indicates "...anyone on MacOS 10.15 (Catalina) and up ...you need to use a .zshenv file instead of .bash_profile. This is because by default since Catalina, the terminal uses zhs instead of bash."
I have no idea if any of the above is true or not, nor if it will help, but there it is...feel free to delete if is of no value.
However, seems awfully complex...
dpb
dpb el 21 de Abr. de 2025
Editada: dpb el 21 de Abr. de 2025
@Walter Roberson, as you are aware I "know nuthink!" about Mac OS X, but curiosity struck...I found an old <Stack Exchange thread> with a relatively late addition to it that indicates "...anyone on MacOS 10.15 (Catalina) and up ...you need to use a .zshenv file instead of .bash_profile. This is because by default since Catalina, the terminal uses zhs instead of bash."
I have no idea if any of the above is true or not, nor if it will help, but there it is...feel free to delete if is of no value/irrelevant.
However, seems awfully complex...
.zshenv and .bash_profile are both related to interactive shells, and are not used when starting applications from icon or dock.
So, you link the icon to a batch file that starts MATLAB from a shell, maybe?
What a mess...apparently at least one thing Bill's Boys did more simply.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 21 de Abr. de 2025
Maybe try using setenv to set it up in your startup.m file.
Walter Roberson
Walter Roberson el 23 de Abr. de 2025
Potentially you could edit the .matlab7rc.sh file. See https://www.mathworks.com/help/matlab/ref/matlabmacos.html#bunueg9-5

3 comentarios

dpb
dpb el 23 de Abr. de 2025
Editada: dpb el 23 de Abr. de 2025
That says .matlab7rc.sh is again only used if start from terminal so leaves one in the same place it appears that the usual from the icon in Applications folder won't show any effect?
I guess your spelunking hasn't uncovered any way for the user to set a persistent environment variable on MacOS, Walter?
Would it work if did the above hack of tying the icon to a batch file that starts MATLAB? I presume that is possible...
In MacOS, starting MATLAB from icon or dock is done by executing the shell script bin/matlab inside the MATLAB installation folder. So a shell script is executed -- just not an interactive shell script.
dpb
dpb el 24 de Abr. de 2025
Editada: dpb el 24 de Abr. de 2025
Ah so..then to fool Father Jobs would require starting terminal and then poking it with the MATLAB command...

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Etiquetas

Preguntada:

MM
el 21 de Abr. de 2025

Editada:

dpb
el 24 de Abr. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by