Java_HOME not found when changed shell from Bash to Zsh on OSX? - macos

This is weird, I have set JAVA_HOME for my mac which can be found when I am using bash shell, but if I change shell, I get a message saying JAVA_HOME not set. What could be going on here?

I stumbled upon your question when trying to solve the same issue while migrating from bash to oh-my-zsh. The reason it's not there is that there is no code setting it for zsh but there was for bash. Generally theres something exporting JAVA_HOME whenever a new bash window is opened so it's always set for you. There is a good thread where this might be happening on the Unix & Linux StackExchange site.
To do the same thing in zsh, you can edit the .zshrc which is run every time zsh starts. I found a sample .zshrc which got me most of the way. The key line being:
export JAVA_HOME=`/usr/libexec/java_home`
Here is the file which I appended to the end of my existing ~/.zshrc file:
#zshrc, interactive shell settings
export ZSH=$HOME/.zsh
# emacs integration
[[ $EMACS = t ]] && unsetopt zle
# env
if [[ -e /usr/libexec/java_home ]]; then
export JAVA_HOME=`/usr/libexec/java_home`
fi
if [[ -e /usr/local/lib/node_modules ]]; then
export NODE_PATH=/usr/local/lib/node_modules
fi
# path
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
export PATH=/opt/usr/sbin:/opt/sbin:/opt/usr/bin:/opt/bin:$PATH
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
export PATH=$HOME/.cabal/bin:$PATH
export PATH=$HOME/.gem/ruby/1.8/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$HOME/.bin:$PATH
setopt null_glob
# source all files in zsh root
for include in $ZSH/*.zsh; do
source $include
done
# source all non-controlled files
for include in $ZSH/private/*.zsh; do
source $include
done
unsetopt null_glob
Then source ~/.zshrc to run in the current shell (or just start a new one) and you should be able to see that it is set with export | grep JAVA_HOME.
I also ended up running mkdir ~/.zsh to create the directory this is looking for and removing the .cabal and .gem lines as they were not needed for me.

I have just installed Mac OS Catalina Version 10.15 and found that environment variables such as JAVA_HOME and others that have been set in my .bash_profile :
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export ANDROID_HOME=/Users/mynziak/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export M2_HOME=/usr/local/Cellar/maven/3.6.2/libexec
export M2=${M2_HOME}/bin
export PATH=${PATH}:${M2_HOME}/bin
are not set in fact!
I saw % in terminal instead of general $ that means you are using a zsh shell instead of bash shell. With Catalina zsh is now the default shell and bash will be completely gone in the future.
oh-my-zsh shell:
https://ohmyz.sh/
So you have to setup all environment variables in .zshrc file.
I just copy-pasted every variables from .bash_profile in to .zshrc and re-opened terminal.
Files .bash_profile and .zshrc are hidden (cmd+shift+. - show hidden files in finder) but can be found in path:
/Users/mynziak/.zshrc
but use own username!

When you set JAVA_HOME in a shell, then it is active and available only for that context, and it will be gone when you close that shell.
Instead either change global environment (or) your .bashrc to include it. So that every time you start a shell, the variable will be available.
edit the .profile or .bash_profile to include the JAVA_HOME.
export JAVA_HOME=`/usr/lib....`
and also below command will return the path for java home directory.
/usr/libexec/java_home -v 1.7
where 1.7 is the version you want.

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
Add above 2 lines in ~/.bashrc or ~/.zshrc and reload the file using source command.

Related

Why do my $PATH environment always reset after I open a new terminal on my mac?

Usually I will nano .zsh_profile
Then I will edit the path
#PYTHON
export PATH=/Users/ffff/Library/Python/3.8/bin:$PATH
# JAVA
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
#ANDROID
export ANDROID_HOME=/Users/ffff/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
then I will save and exit
Then I will
source .zsh_profile
I will test the Java and ADB all is good, but one I open a new Terminal from my mac, it will say ADB and JAVA and Android_HOME not found
Why is it not persist? Did I miss out anything? My mac version is 12.2.1
The .zsh_profile file, has no special meaning. This is probably a confusion form Bash, where the bash_profile is executes for login shells. In Zsh, that equivalent would be ~/.zprofile. You might have some code in your .zshrc file, that overrides the PATH variable with something else. Because, as oppsed to .zprofile, the .zshrc file gets executed for every interactive non-login shells.
So my advice, checkout .zshrc and see if there's something overriding the PATH there, if so, maybe you want to change that, and NOT to execute these commands you want to add for every time a shell is opened, you should put them in .zprofile, so they only get executed once at login.

macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell?

I want to set the environment variable I added below the line to ~/.bash_profile and ~/.profile but it didn't work.
export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
Afterward, exit the terminal and open it again when executing echo $JBOSS_HOME I get nothing.
Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. .bashrc is now .zshrc and .bash_profile is now .zprofile.
If you for some reason (as me) don't want to rename/move your ~/.bash_profile file you can do the next things:
Create a new file ~/.zprofile
Type there source ~/.bash_profile
Save and close
Run a new terminal session
You can just copy your existing bash_profile and name it zprofile and it will work fine.
Run the below command in terminal and you are set after closing and opening new terminal.
cp ~/.bash_profile ~/.zprofile
I created a new file called
/usr/local/bin/mybash
which contains a wrapper script:
/usr/local/bin/bash --init-file $HOME/.bashrc
I installed this local/bin/bash from HomeBrew.
Full Sequence of Events
brew install bash
echo "/usr/local/bin/bash --init-file $HOME/.bashrc" > /usr/local/bin/mybash
chmod +x /usr/local/bin/mybash
Then I opened the settings for terminal.app [cmd-comma]. Under the General Tab, select the radio button for Command (complete path)
In the text box change the text from /bin/zsh/ to /usr/local/bin/bash.
After you close a Terminal window, variables you set in that window are no longer available. If you want the value of a variable to persist across sessions and in all Terminal windows, you must set it in a shell startup script. For information about modifying your zsh shell startup script to keep variables and other settings across multiple sessions, see the “Invocation” section of the zsh man page.
You can use ~/.zlogin to add your variables.
Check out this reference.
changing the bash profile to zsh profile works and source it as well to see in action.
vikas#Vikas-Kumar ~ % mv .bash_profile .zsh_profile
vikas#Vikas-Kumar ~ % source .zsh_profile
You can create a simbolic link and keep your .bash_profile file with this:
ln -s .bash_profile .zsh_profile
source .zsh_profile
Any changes in .bash_profile will be reflected in .zsh_profile
Even with os Catalina /bin/bash comes for free, brew is not needed. Simply create your .bash_profile and set shell in terminal settings to /bin/bash. it automatically finds your .bash_profile. z-shell is not bash-shell and simply renaming will work in most cases but definitely is not correct.
you don't need to update the file, zsh is mac's default, put this in terminal. e.g.:
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
cp zprofile ~/.zprofile
Add to .zprofile:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
eg. by >vi .zprofile
Done

-bash: : command not found when launching the shell in Mac OSX

Lately I tried to made some changes to paths in my .bash_profile.
After I tried to validate these changes with source .bash_profile and got -bash: : command not found, I have also noticed that when I open the shell, before the execution of any command, it stands already "-bash: : command not found"...
Obviously, changes made to .bash_profile weren't taken into consideration by the system niether...
Please, help! I guess that I messed with the .bash_profile too much, have no idea how to fix it...
Contents of .bash_profile:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH=/Users/usrname/TreeTagger/cmd:$PATH
export PATH= /Users/usrname/TreeTagger/bin:$PATH
export UIMA_HOME= $/Users/usrname/apache-uima
export PATH=$PATH:$UIMA_HOME/bin
export HEIDELTIME_HOME='/Users/usrname/Downloads/heideltime-kit/'
export TREETAGGER_HOME='/Users/usrname/TreeTagger'
source $HEIDELTIME_HOME/metadata/setenv
Updated contents of .bash_profile:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH=$PATH:$UIMA_HOME/bin
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
source $HEIDELTIME_HOME/metadata/setenv
Full shell output after last quotes' correction:
Last login: Thu Jul 31 19:07:05 on ttys000
-bash: : command not found
usr:~ usrname$ cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH="$PATH:$UIMA_HOME/bin"
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
source $HEIDELTIME_HOME/metadata/setenv
usr:~ usrname$ source .bash_profile
-bash: : command not found
Edit with echo commands:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH="$PATH:$UIMA_HOME/bin"
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
echo before
source $HEIDELTIME_HOME/metadata/setenv
echo after
usr:~ usrname$ source .bash_profile
-bash: : command not found
usr:~ usrname$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
Edit +bash version:
echo $BASH_VERSION
3.2.51(1)-release
usr:~ usrname$ source ./.bash_profile
-bash: : command not found
Edit +after set -x was added to .bash_profile:
usr:~ usrname$ source ./.bash_profile
++ $'\016'
-bash: : command not found
++ update_terminal_cwd
++ local 'SEARCH= '
++ local REPLACE=%20
++ local PWD_URL=file://usr.local/Users/usrname
++ printf '\e]7;%s\a' file://usr.local/Users/usrname
This line:
export PATH= /Users/usrname/TreeTagger/bin:$PATH
is the problem. In a variable assignment, including an export, you can't have spaces around the =. With the added space, it sets $PATH to the empty string.
Change it to:
export PATH=/Users/usrname/TreeTagger/bin:$PATH
For safety, you should add double quotes:
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
just in case there are any special characters in the value of $PATH.
You have the same problem with UIMA_HOME.
UPDATE :
Based on later comments, you appear to have a stray control character in your .bash_profile. With set -x it's displayed as $'\016'. bash tries to treat that as a command name, and not surprisingly is unable to find it. Edit your .bash_profile and delete that character.
(The other errors needed to be corrected anyway.)

Setting PATH environment variable in OSX permanently

I have read several answers on how to set environment variables on OSX permanently.
First, I tried this, How to permanently set $PATH on Linux/Unix but I had an error message saying no such file and directory, so I thought I could try ~/.bash_profile instead of ~/.profile but it did not work.
Second, I found this solution How to set the $PATH as used by applications in os x , which advises to make changes in
~/.MacOSX/environment.plist
but again I had no such file and directory error.
I need a way to set these variables such that it won't require setting them again and again each time I open a new terminal session.
You have to add it to /etc/paths.
Reference (which works for me) : Here
I've found that there are some files that may affect the $PATH variable in macOS (works for me, 10.11 El Capitan), listed below:
As the top voted answer said, vi /etc/paths, which is recommended from my point of view.
Also don't forget the /etc/paths.d directory, which contains files may affect the $PATH variable, set the git and mono-command path in my case. You can ls -l /etc/paths.d to list items and rm /etc/paths.d/path_you_dislike to remove items.
If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profile or ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH.
If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrc instead of ~/.bash* thing.
And don't forget to restart all the terminal windows, then echo $PATH. The $PATH string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2.
Noticed that the first two ways (/etc/paths and /etc/path.d) is in / directory which will affect all the accounts in your computer while the last two ways (~/.bash* or ~/.zsh*) is in ~/ directory (aka, /Users/yourusername/) which will only affect your account settings.
Read more: Mac OS X: Set / Change $PATH Variable - nixCraft
For a new path to be added to PATH environment variable in MacOS just create a new file under /etc/paths.d directory and add write path to be set in the file. Restart the terminal. You can check with echo $PATH at the prompt to confirm if the path was added to the environment variable.
For example: to add a new path /usr/local/sbin to the PATH variable:
cd /etc/paths.d
sudo vi newfile
Add the path to the newfile and save it.
Restart the terminal and type echo $PATH to confirm
You can open any of the following files:
/etc/profile
~/.bash_profile
~/.bash_login (if .bash_profile does not exist)
~/.profile (if .bash_login does not exist)
And add:
export PATH="$PATH:your/new/path/here"
You could also add this
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
to ~/.bash_profile, then create ~/.bashrc where you can just add more paths to PATH. An example with .
export PATH=$PATH:.
If you are using zsh do the following.
Open .zshrc file nano $HOME/.zshrc
You will see the commented $PATH variable here
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/...
Remove the comment symbol(#) and append your new path using a separator(:) like this.
export
PATH=$HOME/bin:/usr/local/bin:/Users/ebin/Documents/Softwares/mongoDB/bin:$PATH
Activate the change source $HOME/.zshrc
You're done !!!
sudo nano /etc/paths
now find the path of command i am giving an example of setting path for flutter.
/Users/username/development/flutter/bin
now cntrol+x and then y . reopen the terminal and check.
launchctl setenv environmentvariablename environmentvariablevalue
or
launchctl setenv environmentvariablename `command that will generate value`
use proper ` and remember to restart application or terminal for the environment variable to take effect.
you can check environment variable by printenv command.
note : environment variable named path is already set by someone else so we are not appending anything to that path at all here.
shows all hidden files like .bash_profile and .zshrc
$ ls -a
Starting with macOS Catalina, mac uses zsh instead of bash. so by default mac uses zsh.
Check which shell running:
$ echo $SHELL
/usr/zsh
$ cd $HOME
$ open -e .zshrc
or if using vim
$ vi .zshrc
Then add it like this
$ export my_var="/path/where/it/exists"
$ export PATH=$PATH:/$my_var/bin
For example: if installed app named: myapp in /Applications
Then
export MYAPP_HOME=/Applications/myapp
export PATH=$PATH:$MYAPP_HOME/bin
or shortcut
export PATH=${PATH}:/Applications/myapp/bin
TADA you set for life !!! Thank me later
19 October 2021.
Confirming iplus26's answer with one correction.
Test environment
OS: macOS 11.6 (Big Sur) x86_64
Shell: zsh 5.8
Below is the order in which the $PATH environment variable is modified:
each line in etc/paths text file gets appended
each line in each text file inside etc/paths.d directory gets appended
finally, the $PATH is further modified in ~/.zshrc
iplus26's answer stated "when (you run) echo $PATH, The $PATH string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2" but this isn't always the case. It will have to depend on what the script is doing inside .zshrc. E.g. If we do something like
PATH="/new/path:${PATH}"
then, the new path will be in the beginning of the path list. However, if we do something like
PATH="${PATH}:/new/path"
then, the new path will be appended at the end of the path list.
Of course, you'll have to make sure you export the modified path in the ~/.zshrc file.
export PATH=$PATH
One handy command you could use to "pretty print" your path list is
print -l $path
This will print each path on a new line for better readability. Note $path is like $PATH except it's delimited by a single space, instead of a colon, :.
Hopefully this can further clarify for newcomers to this thread.
For setting up path in Mac two methods can be followed.
Creating a file for variable name and paste the path there under
/etc/paths.d and source the file to profile_bashrc.
Export path variable in ~/.profile_bashrc as
export VARIABLE_NAME = $(PATH_VALUE)
AND source the the path. Its simple and stable.
You can set any path variable by Mac terminal or in linux also.

Adding mahout to path in Ubuntu

I am working in Ubuntu 12.04.
I'm a little unsure on what I've done here. About a month ago I downloaded hadoop from Apache's mirrors and just extracted it into usr/local/hadoop. I also installed mahout via maven, to /opt/mahout.
In .bash_profile I have
export HADOOP_CONF_DIR=$HADOOP_HOME/conf
export MAHOUT_HOME=/opt/mahout/bin
export PATH=$PATH:$MAHOUT_HOME
export HADOOP_HOME=/usr/local/hadoop/bin
and at the bottom of .bashrc I have
# Add Hadoop bin/ directory to PATH
export PATH=$PATH:$HADOOP_HOME/bin
# Add Mahout bin/ directory to PATH
export PATH=$PATH:MAHOUT_HOME/bin
Having done all this, if I opened a new terminal window and typed:
echo $HADOOP_HOME
I would get /usr/local/hadoop
And if I typed
echo $MAHOUT_HOME
a blank line was shown. No text.
So today I went back to .bashrc and noticed that I had, at some point, put this in:
# Set Hadoop-related environment variables
export HADOOP_HOME=/usr/local/hadoop
So underneath that I added
# Set Mahout-related environment variables
export MAHOUT_HOME=/opt/mahout/bin
and low and behold, echo $MAHOUT_HOME now generates opt/mahout/bin in the terminal.
So why do some guides say to add export lines to .bash_profile and others say to add to .bashrc? And why would there have been a blank space instead of an error message?
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
So generally the first one is sourced whenever you login (or ssh) the computer as a user, while the other is executed everytime you open a terminal like xterm or gnome-terminal.

Resources